Javascript - 多个复杂的if语句

时间:2010-09-13 16:52:53

标签: javascript

我正在努力解决javaScript中的多个复杂语句,并想知道是否有人能指出我正确的方向。

    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.');
            }
        }

这是我到目前为止所做的,它让我变得灰暗。

4 个答案:

答案 0 :(得分: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 :(得分: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')

答案 2 :(得分:0)

我不确定问题是什么,但你应该使用更一致的缩进,复杂的if语句肯定会变得更清晰。

我的规则:

  • 使用标签根据范围缩进代码行(例如,在花括号内添加+1标签,if语句等)。
  • 不要使用制表符表示其他内容;使用空格进行对齐,必要时使用多个空格(因此,如果语句跨越多行,则使用空格在if语句中排列条件)

答案 3 :(得分: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及其括号{...}

您可以更改for循环以减小代码大小:

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

所以,你不需要反复写这个条件。