2010-09-13 38 views
1

Im在javaScript中使用多個複雜語句掙扎着,想知道是否有人可以指引我朝着正確的方向。Javascript - 多個複雜if語句

function findFlights() 
    { 
    var yourDestination = readTheDestination(); 
    var yourAirline = readTheAirline(); 
    var yourFare = readTheFare(); 


    if (yourDestination == 'Choose your destination') 
     { 
     displayMessage('<B>Please choose a destination city from the Destination menu and then click Find flights again.</B>'); 
     } 
    else 
     { 
     var destinationTime, destinationOperator, destinationFare; 
     var message = '<B>You asked about flights to ' + yourDestination + '</B><BR>' + ""; 

     for (var i=0; i < flightTimes.length; i++)      //flight destinations 
     { 
     if // statement            // IF flight: 
      ((flightDestinations[i] == yourDestination &&    // destination = selected destination & 
      yourAirline == 'Any airline' &&        // airline = any airline & 
      yourFare == 'Any price'))         // fare <= chosen fare 
      ||               // OR 
      (flightDestinations[i] == yourDestination &&    // destination = selected destination & 
      yourAirline == flightOperators[i] &&      // airline = chosen airline & 
      yourFare <= flightFares[i]))        // fare <= chosen fare 

      { 
      destinationTime = flightTimes[i]; 
      destinationOperator = flightOperators[i]; 
      destinationFare = flightFares[i]; 



      message += destinationTime + ' ' + destinationOperator + '. £' + destinationFare + '<BR>'; 
      displayMessage(message); 

      } 
     } 
     else if (flightDestinations[i] == yourDestination && 
       flightOperators[i] != yourAirline && 
       flightFares[i] != yourFare) 
      { 
      displayMessage('There are no flights to ' + yourDestination + ' with ' + yourAirline + '. Please select Any Airline and try again.'); 
      } 
     } 

這是我到目前爲止,它使我灰色。

+7

呃...有一個問題在這裏? – helloandre 2010-09-13 16:57:29

+0

你應該描述它應該做什麼。 – Pointy 2010-09-13 16:57:37

+0

問題'if'陳述數量?或者是否有更好的方法來完成您向我們展示的任務? – Jakub 2010-09-13 17:01:47

回答

0

我不確定問題是什麼,但您應該使用更一致的縮進,複雜的if語句一定會變得更清晰。

我的規則:

  • 使用標籤來根據範圍縮進行代碼(例如+1片而花括號,if報表內等)
  • 不使用標籤爲別的;使用空間進行調準,多個空格(如有必要的話,使用空格排隊在if聲明你的條件,如果該語句跨越多行)
+0

他的壓痕可能是從拷貝粘貼及搞砸了,它看起來像有一種形式縮進。這裏不是一個真正的答案。 – Jakub 2010-09-13 17:02:38

+0

對不起,我的問題是你可以有多少如果/或在聲明中。 – amamam 2010-09-13 17:02:58

+0

我正在處理第二個If語句。我需要它有選定的目的地,選定的航空公司和票價小於或等於選定的數量,它不滿意我在這裏添加的內容。 – amamam 2010-09-13 17:04:39

0

雖然我認爲這不是一個必要的調整,你可以寫代碼如下:

if (yourDestination == 'Choose your destination') { 
    displayMessage('<B>Please choose a destination city from the Destination menu and then click Find flights again.</B>'); 
    return; 
} 

這可以幫助你去除else及其支架{...}

您可以更改爲環,以減少代碼大小:

if(flightDestinations[i] != yourDestination) continue; 

這樣,你不需要反覆寫這個條件。

+0

可以souond蠢,不破意味着該腳本將在這一點上停下來,我們沒有被告知在教程破 – amamam 2010-09-13 17:13:57

+0

不,它不會停止。它只是進入下一個週期而不執行代碼。我遞增,迭代繼續。 – Zafer 2010-09-13 17:24:30

+0

你不是指'繼續'嗎? – MooGoo 2010-09-13 17:32:15

1

你在這裏不匹配您的括號:

((flightDestinations[i] == yourDestination &&    // destination = selected destination & 
yourAirline == 'Any airline' &&        // airline = any airline & 
yourFare == 'Any price'))         // fare <= chosen fare 
||               // OR 
(flightDestinations[i] == yourDestination &&    // destination = selected destination & 
yourAirline == flightOperators[i] &&      // airline = chosen airline & 
yourFare <= flightFares[i]))        // fare <= chosen fare 

變化yourFare == 'Any price'))yourFare == 'Any price')

+0

我知道,我在硬拷貝上更改了它們,只是更容易發佈,我也使用了縮進標籤,因此我可以看到該語句應該放在哪裏。顯然,我們已經使用很老的寫作風格和不知道如果多數民衆贊成什麼造成問題 – amamam 2010-09-13 17:13:12

6

重構複雜的代碼的功能

如果你有複雜的if語句嘗試和功能包起來。所以

(flightDestinations[i] == yourDestination && 
yourAirline == 'Any airline' && 
yourFare == 'Any price') 

可能成爲

function YourDestinationIsTheSameForAnyAirlineOrPrice(flightDestination, yourDestination, yourAirline, yourFare){ 
    return flightDestination == yourDestination && 
     yourAirline == 'Any airline' && 
     yourFare == 'Any price'; 
} 

// And called from if 
if (YourDestinationIsTheSameForAnyAirlineOrPrice(flightDestinations[i], yourDestination, yourAirline, yourFare)) {} 

而不是試圖破譯你有一個函數的名字,告訴你它做什麼的if語句。

使用在多個陣列

具體到你的例子,我也想嘗試,並創建包含目標,時間和航空公司一個航班對象的對象。例如:

var flight = { 
    destination = "London", 
    operator = "BA", 
    time = "18:00 UTC", 
    fare = "£239829" 
} 

這應該使代碼比使用多個數組更可讀。例如:

destinationTime = flightTimes[i]; 
destinationOperator = flightOperators[i]; 
destinationFare = flightFares[i]; 

message += destinationTime + ' ' + destinationOperator + '. £' + destinationFare + '<BR>'; 

// Using an object 
message += flight.Time + ' ' + flight.Operator + '. £' + flight.Fare + '<br />'; 

返回早期

最後,我想盡快擺脫功能。如此使用:

if (yourDestination == 'Choose your destination') { 
    displayMessage('<B>Please choose a destination city from the Destination menu and then click Find flights again.</B>'); 
    return; 
} 

而不是if ... else。我個人覺得這更具可讀性,所以請隨時忽略這一點。

+1

我希望我是從其他來源的學習,但我不得不內限制我的導師的工作,我現在已經學會了一些技巧來改變教我看到的東西會和他們一起去,如果進一步卡住,會再問一次。感謝 – amamam 2010-09-13 17:25:52

+0

伴侶,你忘了把'return'在函數的開始'YourDestinationIsTheSameForAnyAirlineOrPrice'否則這是一個很好的解決方案。 – Puiu 2015-12-10 22:55:47

+0

謝謝@Puiu。好地方! :) – Castrohenge 2015-12-11 11:40:28