2017-05-19 37 views
0

我正在編寫一個程序來顯示總測試等級和avg循環。然而,當我嘗試測試它時,我無法獲得最終消息「Good Job」顯示,並且出現不平衡樹和未定義的「Avg」變量的錯誤。我不明白怎麼「平均」是不確定的,當它經過來自document.write和變量未定義的不平衡樹

var Testscore = 0; 
 
var Testcount = 0; 
 
var Total = 0; 
 
var Avg = 0; 
 
do { 
 
    Testscore = prompt("Enter the test score ", 0); 
 
    Testcount = (Testcount + 1); 
 
    Total = parseFloat(Testscore) + parseFloat(Total); 
 
    Avg = (Total/Testcount); 
 
    document.write("Total test score is " + Total + "<br>"); 
 
    document.write("Average test score is " + Avg + "<br>" + "<br>"); 
 
} while (Testcount < 4) 
 
Avg = (Total/Testcount); 
 
if (avg > 80) { 
 
    document.write("Good Job!"); 
 
} else { 
 
    documet.write("Try harder.."); 
 
}

回答

0

你剛剛在你的代碼的一些錯別字在循環過程的工作只是不!

avgAvg是不同的,因爲變量是case sensitive

此外,還有的documet代替document

var Testscore = 0; 
 
var Testcount = 0; 
 
var Total = 0; 
 
var Avg = 0; 
 
do 
 
    { 
 
    Testscore = prompt("Enter the test score ",0); 
 
    Testcount = (Testcount + 1); 
 
    Total = parseFloat(Testscore) + parseFloat(Total); 
 
    Avg = (Total/Testcount); 
 
    document.write("Total test score is " + Total + "<br>"); 
 
    document.write("Average test score is " + Avg + "<br>" + "<br>"); 
 
    } while (Testcount < 4) 
 
    Avg = (Total/Testcount); 
 
    if (Avg > 80) 
 
     { 
 
      console.log("Good Job!"); 
 
     } 
 
     else 
 
     { 
 
      console.log("Try harder.."); 
 
     }

+0

哦,那好吧平時是怎麼對我來說=),我不知道有關consol.log命令,是有原因我應該在document.write上使用它? – Tstanley12

+0

這主要是爲了演示的目的,但[我會建議不要使用Document.write](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice)。 – Hodrobond

+0

如果你只是試圖在某處看到結果*以便看到它,控制檯比'document.write()'更安全/更一致,我使用它進行測試的主要「icky」是它[清除頁面](http://stackoverflow.com/questions/10873942/document-write-clears-page) – Hodrobond