为什么这个JavaScript程序不起作用?

时间:2014-11-06 17:57:02

标签: javascript

我似乎无法解决这个问题。这是quizResult变量的一个问题,但我不知道如何让程序做我想做的事。我也累了+ = 1,程序根本不会运行。 请帮忙。



//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.");
}
  
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

主要问题是toLowerCase()是一个函数,所以你需要这样调用它。

另一个问题是,无论何时调用变量,都不需要输入var,但这不会导致您的问题。

答案 1 :(得分:0)

看起来你正在与函数toLowerCase进行严格相等的比较,而不是比较toLowerCase()函数的结果,即:

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

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

在这种情况下,您可能只想使用==运算符来检查是否相等,这意味着question1的结果将匹配&#34; 5&#34;和5(因此,如果你不想,你就不需要使用parseInt