var questionsRight 没有添加正确答案
2016-06-24
41
程序没有在页面底部添加正确答案。我不知道为什么,即使 chrome 上的提示回答正确,并且大写,条件语句也不会将它们添加到问题中。
var quiz = [
['What is the capital of New York', 'Albany'],
['What is the Capital of California', 'Sacramento'],
['What is the capital of New Jersey', 'Trenton'],
['What is the capital of Virginia', 'Richmond']
];
var questionsRight = 0;
var questionsWrong = 0;
var questionsRightList = [];
var questionsWrongList = [];
/* Don't NEED to declare the following right away, but it's good practice to go ahead
and declare the variables you're going to use. Notice you don't need to assign any
default value to them */
var question;
var answer;
var response;
var html;
function print(message) {
document.write(message);
}
for( var i = 0; i < quiz.length; i++) {
// Get the first item from array[i] and set it as question
question = quiz[i][0];
// Get the second item from array[i] and set it as answer
answer = quiz[i][1];
// Get input from the user and set it to the response variable
response = prompt(question);
if (question === answer) {
//questionsRight is initially 0. If response === answer questionsRight = 0+1
questionsRight += 1;
}
// else {
// //questionsWrong is initially 0. If response === answer questionsWrong = 0+1
// questionsWrong += 1;
// }
}
html = "You got " + questionsRight + " question(s) right.";
print(html);
2个回答
因为您正在比较(问题 === 答案)。这应该是(响应 === 答案)。
msantos
2016-06-24
条件未使用正确的左赋值。
演示: https://jsfiddle.net/y4orwqqu/
var quiz = [
['What is the capital of New York', 'Albany'],
['What is the Capital of California', 'Sacramento'],
['What is the capital of New Jersey', 'Trenton'],
['What is the capital of Virginia', 'Richmond']
];
var questionsRight = 0;
var questionsWrong = 0;
var questionsRightList = [];
var questionsWrongList = [];
/* Don't NEED to declare the following right away, but it's good practice to go ahead
and declare the variables you're going to use. Notice you don't need to assign any
default value to them */
var question;
var answer;
var response;
var html;
function print(message) {
document.write(message);
}
for( var i = 0; i < quiz.length; i++) {
// Get the first item from array[i] and set it as question
question = quiz[i][0];
// Get the second item from array[i] and set it as answer
answer = quiz[i][1];
// Get input from the user and set it to the response variable
response = prompt(question);
if (response.toLowerCase() === answer.toLowerCase()) {
//questionsRight is initially 0. If response === answer questionsRight = 0+1
questionsRight += 1;
}
// else {
// //questionsWrong is initially 0. If response === answer questionsWrong = 0+1
// questionsWrong += 1;
// }
}
html = "You got " + questionsRight + " question(s) right.";
print(html);
Fernando Chavez Herrera
2016-06-24