我正在努力解决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.');
}
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止,它让我变灰.
重构复杂的代码以实现功能
如果你有复杂的if语句,请尝试将它们包装在函数中.所以
(flightDestinations[i] == yourDestination &&
yourAirline == 'Any airline' &&
yourFare == 'Any price')
Run Code Online (Sandbox Code Playgroud)
可能成为
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)) {}
Run Code Online (Sandbox Code Playgroud)
而不是试图破译if语句,你有一个函数名称告诉你它的作用.
在多个阵列上使用对象
根据您的示例,我还会尝试创建一个包含目的地,时间和航空公司的航班对象.例如:
var flight = {
destination = "London",
operator = "BA",
time = "18:00 UTC",
fare = "£239829"
}
Run Code Online (Sandbox Code Playgroud)
这应该使代码比使用多个数组更具可读性.例:
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 />';
Run Code Online (Sandbox Code Playgroud)
早点回来
最后我会尽快摆脱这个功能.所以使用:
if (yourDestination == 'Choose your destination') {
displayMessage('<B>Please choose a destination city from the Destination menu and then click Find flights again.</B>');
return;
}
Run Code Online (Sandbox Code Playgroud)
而不是if ... else.我个人认为这更具可读性,所以可以随意忽略这一点.