简体   繁体   English

- 帮助创建我的简单 JavaScript 测验?

[英]- Help to create my simple JavaScript quiz?

I'm trying to create an environmentally friendly quiz for kids to take part in as part of my school work.作为我学校作业的一部分,我正在尝试为孩子们创建一个环保测验。 The quiz is supposed to be 6 questions and for every question they answer correct they receive one point, for any question they get wrong their points will just stay the same.测验应该是 6 个问题,他们回答正确的每个问题都会得到一分,对于任何他们答错的问题,他们的分数将保持不变。

My code is not working currently and I cannot figure out why.我的代码目前不工作,我不知道为什么。 I was wondering if I could get some honest input as to how other people would structure this differently or any tips to help me improve.我想知道我是否可以获得一些关于其他人如何以不同方式构建此结构的诚实意见或任何帮助我改进的提示。 Thanks谢谢

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)
  }


}

You seem to be very new at JavaScript. In an if statement, you don't need;. JavaScript你好像很新,在if语句中,不需要;。 (score === +1) isn't a thing, score++ is. (score === +1) 不是问题,score++ 是。 and you wrapped your code in a function which you didn't call.并且您将代码包装在您没有调用的 function 中。 i have edited your code, and it's now working for me.我已经编辑了您的代码,现在可以为我工作了。 here it is:这里是:

JavaScript: 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: 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>

you should call your JavaScript file script.js, and put it in the same folder as the HTML file.你应该调用你的 JavaScript 文件 script.js,并将它放在与 HTML 文件相同的文件夹中。 Otherwise the script tag won't work, and your JavaScript will not run.否则脚本标签将不起作用,您的 JavaScript 将无法运行。

You also said, that if they answer a question wrong, that the points should stay the same.你还说,如果他们回答错了一个问题,分数应该保持不变。 if that's what you want, you can just delete all the score--'s.如果那是你想要的,你可以删除所有的乐谱--'s。 "score--" is the same as "score = score - 1" "score--" 等同于 "score = score - 1"

I have also edited your code, so that the answer is also correct if the user fills in a lowercase letter.我还编辑了您的代码,因此如果用户填写小写字母,答案也是正确的。 "||" “||” means "or" in JavaScript.在 JavaScript 中表示“或”。

Tried this - Hope it helps:) - https://codepen.io/KZJ/pen/LYQYryp试过这个 - 希望它有所帮助:) - https://codepen.io/KZJ/pen/LYQYryp

Only made minor syntax corrections to your code:仅对您的代码进行了较小的语法更正:

  1. Changed score calculation from Comparison operator == to Assignment operator = [Further Reading: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators ]将分数计算从比较运算符 == 更改为赋值运算符 = [进一步阅读:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators ]
  2. Removed semi-colon after the if statements删除了 if 语句后的分号
  3. Called the quiz function quiz();调用了quiz function quiz();
  4. Added case insensitive comparison添加了不区分大小写的比较

Note: Also commented all the negative marking clauses since it was mentioned in the question.注意:还评论了所有负面标记条款,因为它在问题中被提及。

The other two answers here seem to have covered the mistakes in the code, I though I'd point out that you might be able to make things a bit easier to add and edit questions by doing a small amount of refactoring:这里的其他两个答案似乎涵盖了代码中的错误,但我要指出的是,您可以通过进行少量重构来使添加和编辑问题变得更容易一些:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM