[英]Simple Quiz Game JavaScript
I'm just learning now. 我现在正在学习。 Can you please help me, why am I not getting the correct output.
您能帮我吗,为什么我没有得到正确的输出。 This is my code:
这是我的代码:
//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));
}
}
I have watched this code over and over again and I still can't understand why am I getting undefined as a result. 我一遍又一遍地看过这段代码,但我仍然不明白为什么我会变得不确定。 In the debugger, after the loop I check my vars and everything seems ok.
在调试器中,循环后,我检查了我的var,一切似乎都正常。
In your printResult function you are using var i instead of j, 在您的printResult函数中,您使用的是var i而不是j,
Also you better use innerHtml+=message; 另外,您最好使用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>
Your main mistake is using i intead of j: 您的主要错误是使用j的i积分:
for(var j = 0; j < result.length; j++){
oList += "<li>" + result[j] + "</li>";// here was i before
}
Basically you have three problems. 基本上,您有三个问题。
reuse of oList
, the variable should be inside declared and used only in printResult
. 重用
oList
,变量应在内部声明,并且仅在printResult
。
Inside of printResult
, use of i
where j
have been used and 在
printResult
内部,在已使用j
使用i
和
At print
, you replace the actual content with new content. 在
print
,您将实际内容替换为新内容。
Just a small hint with variable names for counting. 只是一个带有变量名的小提示用于计数。 It is good practise to start always with
i
instead of j
and go on with the letters in the alphabet. 优良作法是始终以
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.