gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:01:35 25 4
gpt4 key购买 nike

在了解什么是循环和数组之前,我写了一个小型 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]?或者我应该采取完全不同的方式来解决这个问题?

最佳答案

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

本质上,您循环遍历整个数组,并为每个子项目打印问题的 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]);
}
}

关于javascript - 尝试使用数组和循环重写 JS 测验——正确答案在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37042396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com