请有人解释一下如何让分数变量在每次正确回答问题时添加一个分数变量。每次我运行它时,当我回答正确时,我都会得到零分。谢谢。
var questions = [
['What is 1+1?', 2],
['What is 2+2?', 4],
['What is 4+4?', 8],
];
var score = 0;
for(var i=0; i<questions.length; i++)
{
var response = prompt(questions [i][0]);
var correctAnswer = questions[i][1];
if (response === correctAnswer)
{
score += 1;
}
}
document.write('Your score is '+score);
function print(message) {
document.write(message);
}
最佳答案
parseInt
提示中的结果,因为你想要的是一个数字..
var questions = [
['What is 1+1?', 2],
['What is 2+2?', 4],
['What is 4+4?', 8],
];
var score = 0;
for(var i=0; i<questions.length; i++)
{
var response = parseInt(prompt(questions[i][0]));
var correctAnswer = questions[i][1];
if (response === correctAnswer)
{
score++;
}
}
document.write('Your score is '+score);
function print(message) {
document.write(message);
}
关于JavaScript 变量不会因条件而增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46536622/