简体   繁体   English

Javascript-多个复杂的if语句

[英]Javascript - multiple complex if statements

Im struggling with multiple complex statements in javaScript and wondered if anyone could point me in the right direction. 我正在用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.');
            }
        }

This is what I have so far and its making me gray. 到目前为止,这就是我所拥有的,它使我变得苍白。

Refactor complex code to function 重构复杂代码以起作用

If you have complex if statements try and wrap them up in functions. 如果您有复杂的if语句,请尝试将它们包装在函数中。 So 所以

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

could become 可能成为

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)) {}

Rather than trying to decipher the if statement you have a function name telling you what it does. 而不是试图破译if语句,您可以使用一个函数名称来告诉您它的功能。

Use an object over multiple arrays 在多个数组上使用对象

Specific to your example I would also try and create a single flight object that contains the destination, time and airline. 针对您的示例,我还将尝试创建一个包含目的地,时间和航空公司的单一航班对象。 eg: 例如:

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

This should make the code more readable than using multiple arrays. 这应该比使用多个数组使代码更具可读性。 Example: 例:

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 />';

Return early 早点回来

Finally I would break out of the function as soon as possible. 最后,我将尽快退出该功能。 So use: 因此使用:

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

instead of an if...else. 而不是if ... else。 I personally find this more readable, so feel free to ignore this. 我个人认为这更具可读性,因此可以忽略它。

You've mismatched your parenthesis here: 您此处的括号不匹配:

((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

Change yourFare == 'Any price')) to yourFare == 'Any price') . yourFare == 'Any price'))更改为yourFare == 'Any price')

I'm not sure what the question is, but you should use more consistent indentation and the complex if statements will definitely become clearer. 我不确定问题是什么,但是您应该使用更一致的缩进,并且复杂的if语句肯定会变得更清晰。

My rules: 我的规则:

  • use tabs to indent your lines of code according to scope (eg +1 tab while within curly braces, if statements, etc.) 使用制表符根据范围缩进代码行(例如,花括号中的+1制表符, if语句等)
  • do not use tabs for anything else; 不要将标签用于其他任何用途; use spaces for alignment, multiple spaces if necessary (so, use spaces to line up your conditions in the if statement if the statement spans multiple lines) 使用空格进行对齐,必要时使用多个空格(因此,如果语句跨越多行,请使用空格在if语句中排列条件)

Although I think that this is not a necessary tuning, you can write the code as follows: 尽管我认为这不是必需的调优,但是您可以编写如下代码:

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

This helps you remove the else and its brackets {...} . 这可以帮助您删除else及其括号{...}

You can change the for loop to reduce the code size: 您可以更改for循环以减少代码大小:

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

So that, you don't need to write this condition repeatedly. 因此,您无需重复编写此条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM