簡體   English   中英

- 幫助創建我的簡單 JavaScript 測驗?

[英]- 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

僅對您的代碼進行了較小的語法更正:

  1. 將分數計算從比較運算符 == 更改為賦值運算符 = [進一步閱讀:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators ]
  2. 刪除了 if 語句后的分號
  3. 調用了quiz function quiz();
  4. 添加了不區分大小寫的比較

注意:還評論了所有負面標記條款,因為它在問題中被提及。

這里的其他兩個答案似乎涵蓋了代碼中的錯誤,但我要指出的是,您可以通過進行少量重構來使添加和編輯問題變得更容易一些:

 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