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

Why is this JavaScript program not working?

本文关键字:程序 不起作用 JavaScript 为什么      更新时间:2023-09-26

我似乎想不通。这是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.");
}
  

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

另一个问题是,无论何时调用变量,都不需要键入var,但这并不是问题的根源。

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

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

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

在这种情况下,您可能只想使用==运算符来检查相等性,这意味着question1的结果将同时与"5"和5匹配(因此,如果您不想,则不需要使用parseInt)。