[英]- Help to create my simple JavaScript quiz?
作为我学校作业的一部分,我正在尝试为孩子们创建一个环保测验。 测验应该是 6 个问题,他们回答正确的每个问题都会得到一分,对于任何他们答错的问题,他们的分数将保持不变。
我的代码目前不工作,我不知道为什么。 我想知道我是否可以获得一些关于其他人如何以不同方式构建此结构的诚实意见或任何帮助我改进的提示。 谢谢
function quiz() {
let score = 0
var ques1 = prompt("Which of these can you NOT recycle? \nA - Glass\nB - Paper\nC - Pens and pencils\nAnswer: ")
alert("The answer you have selected is " + ques1);
if (ques1 === "C"); {
(score === +1);
alert("Your answer is correct. Your score is currently " + score);
} else {
(score === 0);
alert("Your answer is not correct")
}
var ques2 = prompt("Which colour bin does paper and cardboard go in? \nA - Blue\nB - Green\nC - Yellow\nAnswer: ")
alert("The answer you have selected is " + ques2);
if (ques2 === "A"); {
(score === +1);
alert("Your answer is correct. Your score is currently " + score);
} else {
(score === -1);
alert("Your answer is not correct")
}
var ques3 = prompt("Which type of transport is best for the environment? \nA - Bus\nB - Car\nC - Bike\nAnswer:")
alert("The answer you have selected is " + ques3);
if (ques3 === "C"); {
(score === +1);
alert("Your answer is correct. Your score is currently " + score);
} else {
(score === -1);
alert("Your answer is not correct")
}
var ques4 = prompt("What is deforestation? \nA - The loss of trees\nB - The loss of clouds\nC - The loss of water\nAnswer:")
alert("The answer you have selected is " + ques4);
if (ques4 === "A"); {
(score === +1);
alert("Your answer is correct. Your score is currently " + score);
} else {
(score === -1);
alert("Your answer is not correct.")
}
var ques5 = prompt("Which of these is a type of green energy? \nA - Petrol\nB - Wind\nC - Wood\nAnswer:")
alert("The answer you have selected is " + ques5);
if (ques5 === "B"); {
(score === +1);
alert("Your answer is correct. Your score is currently " + score);
} else {
(score === -1);
alert("Your answer is not correct.")
}
var ques6 = prompt("When you go to the shop, it's best to... \nA - Buy a paper bag\nB - Buy a plastic bag\nC - Bring your re-usable bag from home\nAnswer:")
alert("The answer you have selected is " + ques6);
if (ques6 === "C"); {
(score === +1);
alert("Your answer is correct. Your score is currently " + score);
} else {
(score === -1);
alert("Your answer is not correct.")
}
if (score >= 3); {
alert("You have passed " + score);
}
else {
alert("You did not pass " + score)
}
}
JavaScript你好像很新,在if语句中,不需要;。 (score === +1) 不是问题,score++ 是。 并且您将代码包装在您没有调用的 function 中。 我已经编辑了您的代码,现在可以为我工作了。 这里是:
JavaScript:
let score = 0
var ques1 = prompt("Which of these can you NOT recycle? \nA - Glass\nB - Paper\nC - Pens and pencils\nAnswer: ")
alert("The answer you have selected is " + ques1);
if (ques1 === "C" || ques1 === "c" {
score++;
alert("Your answer is correct. Your score is currently " + score);
} else {
score = 0;
alert("Your answer is not correct");
}
var ques2 = prompt("Which colour bin does paper and cardboard go in? \nA - Blue\nB - Green\nC - Yellow\nAnswer: ")
alert("The answer you have selected is " + ques2);
if (ques2 === "A" || ques2 === "a") {
score++;
alert("Your answer is correct. Your score is currently " + score);
} else {
score--;
alert("Your answer is not correct")
}
var ques3 = prompt("Which type of transport is best for the environment? \nA - Bus\nB - Car\nC - Bike\nAnswer:")
alert("The answer you have selected is " + ques3);
if (ques3 === "C" || ques3 === "c") {
score++;
alert("Your answer is correct. Your score is currently " + score);
} else {
score--;
alert("Your answer is not correct")
}
var ques4 = prompt("What is deforestation? \nA - The loss of trees\nB - The loss of clouds\nC - The loss of water\nAnswer:")
alert("The answer you have selected is " + ques4);
if (ques4 === "A" || ques4 === "a") {
score++;
alert("Your answer is correct. Your score is currently " + score);
} else {
score--;
alert("Your answer is not correct.")
}
var ques5 = prompt("Which of these is a type of green energy? \nA - Petrol\nB - Wind\nC - Wood\nAnswer:")
alert("The answer you have selected is " + ques5);
if (ques5 === "B" || ques5 === "b") {
score++;
alert("Your answer is correct. Your score is currently " + score);
} else {
score--;
alert("Your answer is not correct.")
}
var ques6 = prompt("When you go to the shop, it's best to... \nA - Buy a paper bag\nB - Buy a plastic bag\nC - Bring your re-usable bag from home\nAnswer:")
alert("The answer you have selected is " + ques6);
if (ques6 === "C" || ques6 === "c") {
score++;
alert("Your answer is correct. Your score is currently " + score);
} else {
score--
alert("Your answer is not correct.")
}
if (score >= 3) {
alert("You have passed with a score of " + score);
} else {
alert("You did not pass with a score of " + score)
}
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="script.js" defer></script>
</head>
<body>
</body>
</html>
你应该调用你的 JavaScript 文件 script.js,并将它放在与 HTML 文件相同的文件夹中。 否则脚本标签将不起作用,您的 JavaScript 将无法运行。
你还说,如果他们回答错了一个问题,分数应该保持不变。 如果那是你想要的,你可以删除所有的乐谱--'s。 "score--" 等同于 "score = score - 1"
我还编辑了您的代码,因此如果用户填写小写字母,答案也是正确的。 “||” 在 JavaScript 中表示“或”。
试过这个 - 希望它有所帮助:) - https://codepen.io/KZJ/pen/LYQYryp
仅对您的代码进行了较小的语法更正:
quiz();
注意:还评论了所有负面标记条款,因为它在问题中被提及。
这里的其他两个答案似乎涵盖了代码中的错误,但我要指出的是,您可以通过进行少量重构来使添加和编辑问题变得更容易一些:
const questions = [ { question: "Which of these can you NOT recycle? \nA - Glass\nB - Paper\nC - Pens and pencils", answer: 'C' }, { question: "Which colour bin does paper and cardboard go in? \nA - Blue\nB - Green\nC - Yellow", answer: 'A' }, { question: "Which type of transport is best for the environment? \nA - Bus\nB - Car\nC - Bike", answer: 'C' }, { question: "What is deforestation? \nA - The loss of trees\nB - The loss of clouds\nC - The loss of water", answer: 'A' }, { question: "Which of these is a type of green energy? \nA - Petrol\nB - Wind\nC - Wood", answer: 'B' }, { question: "When you go to the shop, it's best to... \nA - Buy a paper bag\nB - Buy a plastic bag\nC - Bring your re-usable bag from home", answer: 'C' }, ]; function quiz() { let score = 0; questions.forEach((question) => { let answer = prompt(question.question + "\nAnswer: "); if (answer.toUpperCase() === question.answer) { score += 1; alert("Your answer is correct. Your score is currently " + score); } else { // score -= 1; alert("Your answer is not correct"); } }); if (score >= 3) { alert("You have passed " + score); } else { alert("You did not pass " + score) } } quiz();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.