2013-03-15 6 views
0

샌드 박스 환경에서이 코드 세그먼트를 실행할 때마다 시간이 초과되어 결국 중단됩니다. 내가 여러 IDE를 통해 그것을 실행하고 여전히 구문 오류를 찾을 수 없습니다. 아무도 내가 아닌 것을보고 있다면, 나는 그 의견을 매우 고맙게 생각합니다.자바 스크립트 시간 초과

//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 (prompt ("이 차량으로 을 몇 마일 만 주행 시켰습니까?"));'실제로 그런 식으로 깨졌습니까? 나쁜 라인을 거기에 고치고 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."); 
} 

변경 사항을 적용했을 때 효과가있었습니다.

관련 문제