如果错误3次..游戏结束了



我正在做一个琐事(使用API(和游戏中的第三个错误答案,但我只写了一个错误答案。你知道用三个错误的答案怎么做吗?

choices.forEach(choice => {
choice.addEventListener("click", e => {
if (!acceptingAnswers) return;
acceptingAnswers = false;
const selectedChoice = e.target;
const selectedAnswer = selectedChoice.dataset["number"];
const classToApply =
selectedAnswer == currentQuestion.answer ? "correct" : "incorrect";
if (classToApply === "correct") {
incrementScore(CORRECT_BONUS);
} else if (classToApply === "incorrect") {
incrementScore( INCORRECT_BONUS);
}

//如果错误3次。。。游戏结束

if ((classToApply === "incorrect")){
return window.location.assign("/html/end.html");
}
selectedChoice.parentElement.classList.add(classToApply);
setTimeout(() => {
selectedChoice.parentElement.classList.remove(classToApply);
getNewQuestion();
}, 1000);
});
});
incrementScore = num => {
score += num;
scoreText.innerText = score;
};

您只需要统计有多少错误并采取相应的行动。

let wrongAnswers = 0;
choices.forEach(choice => {
choice.addEventListener("click", e => {
if (!acceptingAnswers) return;
acceptingAnswers = false;
const selectedChoice = e.target;
const selectedAnswer = selectedChoice.dataset["number"];
const classToApply =
selectedAnswer == currentQuestion.answer ? "correct" : "incorrect";
if (classToApply === "correct") {
incrementScore(CORRECT_BONUS);
} else if (classToApply === "incorrect") {
incrementScore( INCORRECT_BONUS);
wrong++;
if(wrong >= 3){
window.location.assign("/html/end.html");
}
}

最新更新