Javascript - 多个复杂的 if 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3702598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - multiple complex if statements
提问by amamam
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.
这就是我到目前为止所拥有的,它让我变灰。
回答by Castrohenge
Refactor complex code to function
重构复杂的代码以发挥作用
If you have complex if statements try and wrap them up in functions. So
如果您有复杂的 if 语句,请尝试将它们包装在函数中。所以
(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. I personally find this more readable, so feel free to ignore this.
而不是 if...else。我个人认为这更具可读性,所以请随意忽略这一点。
回答by palswim
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')。
回答by palswim
I'm not sure what the question is, but you should use more consistent indentation and the complex ifstatements will definitely become clearer.
我不确定问题是什么,但你应该使用更一致的缩进,复杂的if语句肯定会变得更清晰。
My rules:
我的规则:
- use tabs to indent your lines of code according to scope (e.g. +1 tab while within curly braces,
ifstatements, etc.) - 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
ifstatement if the statement spans multiple lines)
- 使用制表符根据范围缩进您的代码行(例如,在大括号、
if语句等内时 +1 制表符) - 不要将标签用于其他任何事情;使用空格对齐,必要时使用多个空格(因此,
if如果语句跨越多行,请使用空格来排列语句中的条件)
回答by Zafer
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 elseand 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.
这样,您就不需要重复编写此条件。

