2013-03-15 2 views
0

Каждый раз, когда я запускаю этот сегмент кода в среде песочницы, я тайм-аут и в конечном итоге сбой. Я запустил его через mutliple IDE и до сих пор не могу найти никаких синтаксических ошибок. Если кто-то увидит то, чего я не знаю, я очень благодарен за вход.Javascript time-out

//assign variables to memory 
var carModel = new Array() 
var milesDriven = new Array() 
var gallonsUsed = new Array() 
var milesperGallon = new Array() 
var questionState, epaRating, conditionofVehicle, totalAverage; 

var runningAverage = 0; 
var i = 0; 

do { 
    //retrieve basic information about vehicle from standard input: model, miles, gallons 
    carModel[i] = prompt("What type of vehicle do you drive?"); 
    milesDriven[i] = parseInt(prompt("How many miles have you 
            driven with this vehicle?")); 
    gallonsUsed[i] = parseInt(prompt("How many gallons of gas have you used when driving this vehicle?")); 

    //calculate miles per gallon 
    milesperGallon[i] = milesDriven[i]/gallonsUsed[i]; 
    runningAverage = runningAverage + milesperGallon[i]; 

    do { 
     questionState = prompt("Do you have any more vehicles that you would like to enter? If yes, type 'yes'. If no, type 'no'."); 
    } while (questionState != "yes" && questionState != "no"); 

    i++; 
} while (questionState == "yes"); 

totalAverage = runningAverage/i; 

// parse through each element of carModel, milesDriven, gallonsUsed 
for (var j = 0; j <= i; j++) { 
    //determine performance standards based upon M.P.G of vehicle 
    if (milesperGallon[j] >= 38) { 
     conditionofVehicle = "Good"; 
     epaRating = 10; 
     //output the performance standards to user 
     document.writeln("Your" + " " + carModel[j] + " " + "receives" + " " + milesperGallon[j] + " " + "miles per gallon."); 
     document.writeln("Based upon this metric your vehicle is in" + " " + conditionofVehicle + " " + "condition and has a EPA rating of" + " " + epaRating + "."); 
    } else if (milesperGallon[j] >= 31 && milesperGallon[j] < 38) { 
     conditionofVehicle = "Good"; 
     epaRating = 9; 
     //output the performance standards to user 
     document.writeln("Your" + " " + carModel[j] + " " + "receives" + " " + milesperGallon[j] + " " + "miles per gallon."); 
     document.writeln("Based upon this metric your vehicle is in" + " " + conditionofVehicle + " " + "condition and has a EPA rating of" + " " + epaRating + "."); 
    } else if (milesperGallon[j] >= 27 && milesperGallon[j] < 31) { 
     conditionofVehicle = "Good"; 
     epaRating = 8; 
     //output the performance standards to user 
     document.writeln("Your" + " " + carModel[j] + " " + "receives" + " " + milesperGallon[j] + " " + "miles per gallon."); 
     document.writeln("Based upon this metric your vehicle is in" + " " + conditionofVehicle + " " + "condition and has a EPA rating of" + " " + epaRating + "."); 
    } else if (milesperGallon[j] >= 23 && milesperGallon[j] < 27) { 
     conditionofVehicle = "Fair"; 
     epaRating = 7; 
     //output the performance standards to user 
     document.writeln("Your" + " " + carModel[j] + " " + "receives" + " " + milesperGallon[j] + " " + "miles per gallon."); 
     document.writeln("Based upon this metric your vehicle is in" + " " + conditionofVehicle + " " + "condition and has a EPA rating of" + " " + epaRating + "."); 
    } else if (milesperGallon[j] >= 22 && milesperGallon[j] < 23) { 
     conditionofVehicle = "Fair"; 
     epaRating = 6; 
     //output the performance standards to user 
     document.writeln("Your" + " " + carModel[j] + " " + "receives" + " " + milesperGallon[j] + " " + "miles per gallon."); 
     document.writeln("Based upon this metric your vehicle is in" + " " + conditionofVehicle + " " + "condition and has a EPA rating of" + " " + epaRating + "."); 
    } else if (milesperGallon[j] >= 19 && milesperGallon[j] < 22) { 
     conditionofVehicle = "Fair"; 
     epaRating = 5; 
     //output the performance standards to user 
     document.writeln("Your" + " " + carModel[j] + " " + "receives" + " " + milesperGallon[j] + " " + "miles per gallon."); 
     document.writeln("Based upon this metric your vehicle is in" + " " + conditionofVehicle + " " + "condition and has a EPA rating of" + " " + epaRating + "."); 
    } else if (milesperGallon[j] >= 17 && milesperGallon[j] < 19) { 
     conditionofVehicle = "Fair"; 
     epaRating = 4; 
     //output the performance standards to user 
     document.writeln("Your" + " " + carModel[j] + " " + "receives" + " " + milesperGallon[j] + " " + "miles per gallon."); 
     document.writeln("Based upon this metric your vehicle is in" + " " + conditionofVehicle + " " + "condition and has a EPA rating of" + " " + epaRating + "."); 
    } else if (milesperGallon[j] >= 15 && milesperGallon[j] < 17) { 
     conditionofVehicle = "Bad"; 
     epaRating = 3; 
     //output the performance standards to user 
     document.writeln("Your" + " " + carModel[j] + " " + "receives" + " " + milesperGallon[j] + " " + "miles per gallon."); 
     document.writeln("Based upon this metric your vehicle is in" + " " + conditionofVehicle + " " + "condition and has a EPA rating of" + " " + epaRating + "."); 
    } else if (milesperGallon[j] >= 13 && milesperGallon[j] < 15) { 
     conditionofVehicle = "Bad"; 
     epaRating = 2; 
     //output the performance standards to user 
     document.writeln("Your" + " " + carModel[j] + " " + "receives" + " " + milesperGallon[j] + " " + "miles per gallon."); 
     document.writeln("Based upon this metric your vehicle is in" + " " + conditionofVehicle + " " + "condition and has a EPA rating of" + " " + epaRating + "."); 
    } else if (milesperGallon[j] >= 0 && milesperGallon[j] < 13) { 
     conditionofVehicle = "Bad"; 
     epaRating = 1; 
     //output the performance standards to user 
     document.writeln("Your" + " " + carModel[j] + " " + "receives" + " " + milesperGallon[j] + " " + "miles per gallon."); 
     document.writeln("Based upon this metric your vehicle is in" + " " + conditionofVehicle + " " + "condition and has a EPA rating of" + " " + epaRating + "."); 
    } else { 
     document.writeln(" "); 
    } 
} 
// check to make sure there is an input value 
if (totalAverage >= 0) { 
    document.writeln("You have" + " " + i + " " + "vehicles." + " " + "Between them  
        they average" + " " + totalAverage + " " + "miles per gallon."); 
} 
+0

Является ли линия 'milesDriven [i] = parseInt (подсказка (« Сколько миль у вас есть , приводимый в действие этим транспортным средством? »));' Действительно разрушен таким образом? Кажется, что он отлично работает, как только вы исправляете неисправную линию, ломающуюся там, и в конце в состоянии if. – j08691

+0

Это была проблема форматирования с переполнением стека. мой исходный код не включает эти разрывы строк. –

+1

Странно, потому что, когда вы исправляете это, кажется, все нормально: http://jsfiddle.net/j08691/VwbwA/ – j08691

ответ

3

Вы не можете разбить строку на нескольких строках:

milesDriven[i] = parseInt(prompt("How many miles have you 
           driven with this vehicle?")); 

Оно должно быть:

milesDriven[i] = parseInt(prompt("How many miles have you driven with this vehicle?")); 

Аналогично с этой линии:

if (totalAverage >= 0) { 
    document.writeln("You have" + " " + i + " " + "vehicles." + " " + "Between them  
        they average" + " " + totalAverage + " " + "miles per gallon."); 
} 

должен быть :

if (totalAverage >= 0) { 
    document.writeln("You have" + " " + i + " " + "vehicles." + " " + "Between them they average" + " " + totalAverage + " " + "miles per gallon."); 
} 

Это работало для меня, когда я вносил эти изменения.