开发者问题收集

尝试使用数组和循环重写 JS 测验——正确答案放在哪里?

2016-05-05
1135

在我了解循环和数组是什么之前,我写了一个小型 JS 测验,现在我正试图将它整理一下。原始测验在警告窗口中向用户提问,如果他们回答不正确,它会告诉他们正确答案是什么。如果答案正确,它会告诉他们答案正确,然后再继续下一个问题。它还会记录用户答对或答错的次数。这是我的原始(过多)代码:

var correct = 0;

var answer1 = prompt("What is the capital of England?");
  if (answer1.toUpperCase() === "LONDON") {
    alert("That's correct!")
    correct += 1
  }
  else {
    alert("False. The correct answer is London.")
  }

var answer2 = prompt("What is the capital of France?")
   if (answer1.toUpperCase() === "PARIS") {
    alert("That's correct!")
    correct += 1
  }
  else {
    alert("False. The correct answer is Paris.");
  }

var answer3 = prompt("What is the capital of Canada?")
  if (answer3.toUpperCase() === "OTTAWA") {
    alert("That's correct!");
    correct += 1
  }
  else {
    alert("False. The correct answer is Ottawa.");
  }

document.write("<h1>You answered " + correct + " out of 5 questions correctly.</h1>")

if (correct === 5) {
  document.write("You won a gold star!");
}
  else if (correct >= 3) {
  document.write("You won a silver star.");
}

  else if (correct >= 2) {
  document.write ("You win third place.");
}

else {
  document.write("Sadly, you did not win any stars.");
}

如您所见,这非常长且很菜鸟。以下是我一直在努力重写的版本:

var questions = [
["What is the capital of England?", "LONDON"]
["What is the capital of France?", "PARIS"]
["What is the capital of Canada?", "OTTAWA"]
]

var correctAnswers = 0; 

for (var i = 0; i < questions.length ; i += 1 ) {
question = questions[i][0];
answer = questions[i][1];
response = prompt(question);
response = toUpperCase(response);
if response === answer {
correctAnswers += 1;
}

我有点困惑的是正确答案的结构和位置,如果用户回答不正确,测验会在警告窗口中向用户显示这些答案。我是否需要向二维数组添加第三列,然后在 for 循环中引用它,即 correctResponse = [i][2]?或者我应该采用完全不同的方式来实现这一点?

1个回答

您只是缺少一些逗号,数组就乱了。您不需要再使数组结构复杂化了。不过,您各个数组项之间没有逗号。

本质上,您循环遍历整个数组,对于每个子项,您打印出问题的 questions[i][0] 和答案的 questions[i][1]

var questions = [
["What is the capital of England?", "LONDON"],
["What is the capital of France?", "PARIS"],
["What is the capital of Canada?", "OTTAWA"]
];

var correctAnswers = 0;

for (var i = 0; i < questions.length; i++) {
    var answer = prompt(questions[i][0]);
    if (answer.toUpperCase() == questions[i][1]) {
        alert("Correct!");
        correctAnswers++;
    }
    else {
        alert("incorrect, the correct answer was " + questions[i][1]);
    }
}

如果他们答对了问题,则使用自定义响应的示例: 您只需向每个问题数组添加另一个项,并且只有在他们答对了问题时才显示该项。

var questions = [
["What is the capital of England?", "LONDON", "You know, where the queen lives"],
["What is the capital of France?", "PARIS", "Remember the eiffel tower?"],
["What is the capital of Canada?", "OTTAWA", "Lot of mooses there"]
];

var correctAnswers = 0;

for (var i = 0; i < questions.length; i++) {
    var answer = prompt(questions[i][0]);
    if (answer.toUpperCase() == questions[i][1]) {
        alert("Correct! " + questions[i][2]);
        correctAnswers++;
    }
    else {
        alert("incorrect, the correct answer was " + questions[i][1]);
    }
}
Keatinge
2016-05-05