[英]Javascript simple if/else statement Quiz Game with 5 questions that outputs results at the end
[英]Simple Quiz Game JavaScript
我現在正在學習。 您能幫我嗎,為什么我沒有得到正確的輸出。 這是我的代碼:
//ask questions
var quiz = [
["When is Bulgaria established?", 681],
["What year was it before 16 years?", 2000],
["When does WWII ends?", 1945]
];
//variables
var answer = [];
var correct = [];
var wrong = [];
var correctAns = 0;
var wrongAns = 0;
var oList = "<ol>";
//function to print the result in ordered list
function printResult(result){
for(var j = 0; j < result.length; j++){
oList += "<li>" + result[i] + "</li>";
}
oList += "</ol>";
return oList;
}
function print(message) {
document.getElementById('output').innerHTML = message;
}
//looping, adding correct and wrong answeres
for(var i = 0; i < 3; i++) {
answer[i] = prompt(quiz[i][0]);
if(parseInt(answer[i]) == quiz[i][1]){
correct.push(quiz[i][0]);
correctAns++;
} else {
wrong.push(quiz[i][0]);
wrongAns++;
}
}
//print logic
if(correct.length < 1 || correct == undefined){
print("You did not guess any of the quiestions!");
} else if (correct.length >= 1){
print("You have guessed " + correctAns + " questions.");
print(printResult(correct));
print("You have " + wrongAns + " wrong answeres.");
if(wrongAns > 0){
print(printResult(wrong));
}
}
我一遍又一遍地看過這段代碼,但我仍然不明白為什么我會變得不確定。 在調試器中,循環后,我檢查了我的var,一切似乎都正常。
在您的printResult函數中,您使用的是var i而不是j,
另外,您最好使用innerHtml + = message;
//ask questions var quiz = [ ["When is Bulgaria established?", 681], ["What year was it before 16 years?", 2000], ["When does WWII ends?", 1945] ]; //variables var answer = []; var correct = []; var wrong = []; var correctAns = 0; var wrongAns = 0; //function to print the result in ordered list function printResult(result){ //HERE: var oList = "<ol>"; for(var j = 0; j < result.length; j++){ oList += "<li>" + result[j] + "</li>"; } oList += "</ol>"; return oList; } function print(message) { document.getElementById('output').innerHTML += message; } //looping, adding correct and wrong answeres for(var i = 0; i < 3; i++) { answer[i] = prompt(quiz[i][0]); if(parseInt(answer[i]) == quiz[i][1]){ correct.push(quiz[i][0]); correctAns++; } else { wrong.push(quiz[i][0]); wrongAns++; } } //print logic if(correct.length < 1 || correct == undefined){ print("You did not guess any of the quiestions!"); } else if (correct.length >= 1){ print("You have guessed " + correctAns + " questions."); print(printResult(correct)); print("You have " + wrongAns + " wrong answeres."); if(wrongAns > 0){ print(printResult(wrong)); } }
<div id="output"> </div>
您的主要錯誤是使用j的i積分:
for(var j = 0; j < result.length; j++){
oList += "<li>" + result[j] + "</li>";// here was i before
}
基本上,您有三個問題。
重用oList
,變量應在內部聲明,並且僅在printResult
。
在printResult
內部,在已使用j
使用i
和
在print
,您將實際內容替換為新內容。
只是一個帶有變量名的小提示用於計數。 優良作法是始終以i
而不是j
開頭,並繼續字母中的字母。
var quiz = [["When is Bulgaria established?", 681], ["What year was it before 16 years?", 2000], ["When does WWII ends?", 1945]], answer = [], correct = [], wrong = [], correctAns = 0, wrongAns = 0; //function to print the result in ordered list function printResult(result) { var oList = "<ol>"; // !!! move variable inside of the function for (var j = 0; j < result.length; j++) { oList += "<li>" + result[j] + "</li>"; // !!! use j indstead if i } oList += "</ol>"; return oList; } function print(message) { document.getElementById('output').innerHTML += message; // !!! append message } //looping, adding correct and wrong answeres for (var i = 0; i < 3; i++) { answer[i] = prompt(quiz[i][0]); if (parseInt(answer[i]) == quiz[i][1]) { correct.push(quiz[i][0]); correctAns++; } else { wrong.push(quiz[i][0]); wrongAns++; } } //print logic if (correct.length < 1 || correct == undefined) { print("You did not guess any of the quiestions!"); } else if (correct.length >= 1) { print("You have guessed " + correctAns + " questions."); print(printResult(correct)); print("You have " + wrongAns + " wrong answeres."); if (wrongAns > 0) { print(printResult(wrong)); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.