2014-11-06 33 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) 。