我目前正在嘗試做一個測驗,因爲問題一次只顯示一個問題,總的來說,用戶只需在允許進展前正確回答3個問題。在任何時候,如果用戶錯誤地回答任何問題,用戶將被引導至GameOver頁面。Javascript測驗不進行到下一個
我目前設法設置隨機問題出現,當用戶選擇錯誤答案時,他們將被定向到GameOver頁面。
此時,即使用戶選擇了正確的答案,我仍被卡在始終顯示Question1的部分。
當用戶從當前問題中選擇正確的答案時,我可以獲得一些關於如何調用下一個問題的幫助嗎?
感謝
var questionOrder = ["Q1", "Q2", "Q3"];
var answerOrder = [
["Yes", "No"],
["Yes", "No"],
["Yes", "No"]
];
var CorrectAnswers = ["1", "2", "2"];
//To Set random Question
var random_QuestionIndex = Math.floor(Math.random() * questionOrder.length);
//Assign Variable to generate random question for the quiz
var question = questionOrder[random_QuestionIndex];
var answerList = answerOrder[random_QuestionIndex];
function showQuestion() {
//Randomise Question
//Code to generate random question for the quiz
AnswerIndex = "";
$('#Question_List').html(question);
//Generate random answers in relation to the questions
$('#STBAnswer_01').hide();
$('#STBAnswer_02').hide();
$('#STBAnswer_03').hide();
//$('#STBAnswer_04').hide();
for (i = 0; i < answerList.length; i++) {
$('#STBAnswer_0' + (i + 1)).attr('src', answerList[i]);
$('#STBAnswer_0' + (i + 1)).show();
}
}
function selectSTBAnswer(index) {
AnswerIndex = index + "";
console.log(AnswerIndex);
console.log(CorrectAnswers[random_QuestionIndex]);
//Conditional Answer Check; if answer is wrong, GameOver, else proceed to next Question
if (CorrectAnswers[random_QuestionIndex] != AnswerIndex) {
console.log("IncorrectAnswer");
$('#QnA').fadeOut({
duration: slideDuration,
queue: false
});
$('#GameOver').fadeIn({
duration: slideDuration,
queue: false
});
} else {
console.log("CorrectAnswer");
for (i = 0; i < 3; i++) {
$('#Question_List').fadeOut();
$('#STBAnswer_01').fadeOut();
$('#STBAnswer_02').fadeOut();
$('#STBAnswer_03').fadeOut();
}
}
}
<div id="QnA" align="center" style="position:absolute; height:1920px; width:1080px; background-repeat: no-repeat; z-index=1; top:0px; left:0px; ">
<div id="Question_List" style="position:absolute; z-index=99; width:900px; height:200px; top:50px; left:100px; font-size:45px; font-family:'HelveticaNeue'; color:#fff;"></div>
<img id="STBAnswer_01" style="z-index=99; position:absolute; top:250px; left:50px; border:0px;" onclick="selectSTBAnswer('1');">
<img id="STBAnswer_02" style="z-index=99; position:absolute; top:350px; left:50px; border:0px;" onclick="selectSTBAnswer('2');">
<img id="STBAnswer_03" style="z-index=99; position:absolute; top:450px; left:50px; border:0px;" onclick="selectSTBAnswer('3');">
</div>
<div id="GameOver" align="center" style="position:absolute; height:1920px; width:1080px; background-repeat: no-repeat; display: none; z-index=4; top:0px; left:0px; ">
GAME OVER
</div>
我被困當用戶選擇了正確答案爲,它不會轉換到下一個問題,但停留在當期的第一個問題。
請幫忙。謝謝。
所以我有的循環是絕對多餘的 – Luke
什麼是'var myquiz = myquiz || {...};'。你爲什麼需要這樣做?你設置的那個和我設置的全局變量有什麼區別? – Luke
如果先前存在該段,則該段不會替換舊段。在這種情況下不是很大的交易。 –