2014-11-06 110 views
-2

我似乎無法弄清楚這一點。這是與quizResult變量的問題,但我不知道如何讓程序做我想做的事情。我也厭倦了+ = 1,程序根本不會運行。 請幫忙。這個JavaScript程序爲什麼不起作用?

//Five question quiz using prompt, result at the end, and will be ranked 
 

 
/*Questions*/ 
 
var question1 = prompt("What does 2 + 2 equal?"); 
 
var question2 = prompt("Name one of the five greatest rappers of all time"); 
 
var question3 = prompt("Fill in the blank- I'll be ____"); 
 
var question4 = prompt("What programming language are we using?"); 
 
var question5 = prompt("Are you alive?"); 
 

 
/*Counter*/ 
 
var quizResult = 0; 
 

 
/*Conditionals*/ 
 
if(parseInt(question1) === 4){ 
 
    var quizResult = quizResult +1; 
 
} 
 
if(question2.toLowerCase === "dylon"){ 
 
    var quizResult = quizResult +1; 
 
} 
 
if(question3.toLowerCase === "back"){ 
 
    var quizResult = quizResult +1; 
 
} 
 
if(question4.toLowerCase === "javascript"){ 
 
    var quizResult = quizResult +1; 
 
} 
 
if(question5.toLowerCase === "yes"){ 
 
    var quizResult = quizResult +1; 
 
} 
 

 
/*Display Reslut to user*/ 
 
if(quizResult === 5){ 
 
    document.write("You answered " + quizResult + " correctly. You recieve the gold crown."); 
 
}else if(quizResult >= 3 && quizResult <= 4){ 
 
    document.write("You answered " + quizResult + " correctly. You recieve the silver crown."); 
 
}else if(quizResult >= 1 && quizResult <= 2){ 
 
    document.write("You answered " + quizResult + " correctly. You recieve the bronze crown."); 
 
}else{ 
 
    document.write("You answered " + quizResult + " correctly. Congratulations, you are not that bright."); 
 
} 
 

+4

用'quizResult ++'替換整行,不要每次重新定義變量。 – adeneo 2014-11-06 17:58:31

+2

'.toLowerCase'是一個功能!使用它作爲'.toLowerCase()' – Cheery 2014-11-06 17:59:19

+0

這個問題似乎是無關緊要的,因爲它有太多的錯誤來解釋它們,你需要返回並獲得一個好的JavaScript教程。這超出了**太廣泛**。 – 2014-11-06 18:03:44

回答

0

的主要問題是,toLowerCase()是一個函數,所以你需要調用它本身。

另一個問題是,當你調用一個變量時,你不需要輸入var,但這不會導致你的問題。

0

看起來你正在做與功能toLowerCase嚴格的平等的比較,而不是比較的結果與toLowerCase()函數,即:

if(question4.toLowerCase === "javascript") 

if(question4.toLowerCase() === "javascript") 

在在這種情況下,您可能只想使用==運算符來檢查相等性,這意味着question1的結果將匹配「5」和5(因此,如果您不想要,則不需要使用parseInt) 。