Javascript中的askQuestion()函数总是将答案标记为错误
代码笔中指向我的笔的链接- Javascript在这里:Javascript中的askQuestion()函数总是将答案标记为错误,javascript,jquery,html,css,Javascript,Jquery,Html,Css,代码笔中指向我的笔的链接- Javascript在这里: function randomNum(digits) { return Math.floor(Math.pow(10, digits - 1) + Math.random() * 9 * Math.pow(10, digits - 1)); } function askQuestion(digits) { $(".result").html(""); var factor1 = randomNum(digits
function randomNum(digits) {
return Math.floor(Math.pow(10, digits - 1) + Math.random() * 9 * Math.pow(10, digits - 1));
}
function askQuestion(digits) {
$(".result").html("");
var factor1 = randomNum(digits);
var factor2 = randomNum(digits);
var correctanswer = factor1 * factor2;
var answer = parseInt($(".answer").val(), 10);
console.log(correctanswer);
$(".question").html(factor1 + " × " + factor2);
var score = 0;
//Problem Starts Here
$(".check").click(function() {
if (correctanswer == answer) {
$(".result").html("Correct");
score += 1;
$(".score").html(score);
askQuestion(digits);
} else {
$(".result").html("Wrong");
score -= 1;
$(".score").html(score);
}
});
//Problem Ends Here
}
$(document).ready(function() {
$(".answer").hide();
$(".check").hide();
var digits = 0;
$(".digits-1").click(function() {
digits += 1;
});
$(".digits-2").click(function() {
digits += 2;
});
$(".digits-3").click(function() {
digits += 3;
});
$(".btn").click(function() {
$(".btn").hide();
$(".answer").show();
$(".check").show();
askQuestion(digits);
});
});
在这些评论之间,我认为问题出在哪里。例如,当它要求4 x 9,而我输入36时,它仍然将其标记为错误。我不知道为什么会这样。起初,我认为输入的信息可能仍然是一个字符串,所以我对其使用了parseInt,但它仍然不起作用。感谢所有的帮助。提前谢谢 问题在于,您正在将$'.answer'元素的初始值缓存在您的answer变量中。当您执行$'.answer.val时,它会保存当时存在的内容,因此如果用户随后更改其答案,它将不会反映在您的变量中。您要做的是这样的:
// Rest of your code above
var answer = $(".answer");
console.log(correctanswer);
$(".question").html(factor1 + " × " + factor2);
var score = 0;
//Problem Starts Here
$(".check").click(function() {
// Don't check what is in the input until you're ready to use the value.
if (correctanswer == parseInt(answer.val(), 10)) {
$(".result").html("Correct");
score += 1;
$(".score").html(score);
askQuestion(digits);
} else {
$(".result").html("Wrong");
score -= 1;
$(".score").html(score);
}
});
$(".check").click(function() {
var answer = parseInt($(".answer").val(), 10);
if (correctanswer == answer) {
$(".result").html("Correct");
score += 1;
$(".score").html(score);
askQuestion(digits);
} else {
$(".result").html("Wrong");
score -= 1;
$(".score").html(score);
}
});
问题是您正在缓存应答变量中$'.answer'元素的初始值。当您执行$'.answer.val时,它会保存当时存在的内容,因此如果用户随后更改其答案,它将不会反映在您的变量中。您要做的是这样的:
// Rest of your code above
var answer = $(".answer");
console.log(correctanswer);
$(".question").html(factor1 + " × " + factor2);
var score = 0;
//Problem Starts Here
$(".check").click(function() {
// Don't check what is in the input until you're ready to use the value.
if (correctanswer == parseInt(answer.val(), 10)) {
$(".result").html("Correct");
score += 1;
$(".score").html(score);
askQuestion(digits);
} else {
$(".result").html("Wrong");
score -= 1;
$(".score").html(score);
}
});
$(".check").click(function() {
var answer = parseInt($(".answer").val(), 10);
if (correctanswer == answer) {
$(".result").html("Correct");
score += 1;
$(".score").html(score);
askQuestion(digits);
} else {
$(".result").html("Wrong");
score -= 1;
$(".score").html(score);
}
});
单击处理程序具有对旧答案值的引用。每次都必须从输入中获取值。大概是这样的:
// Rest of your code above
var answer = $(".answer");
console.log(correctanswer);
$(".question").html(factor1 + " × " + factor2);
var score = 0;
//Problem Starts Here
$(".check").click(function() {
// Don't check what is in the input until you're ready to use the value.
if (correctanswer == parseInt(answer.val(), 10)) {
$(".result").html("Correct");
score += 1;
$(".score").html(score);
askQuestion(digits);
} else {
$(".result").html("Wrong");
score -= 1;
$(".score").html(score);
}
});
$(".check").click(function() {
var answer = parseInt($(".answer").val(), 10);
if (correctanswer == answer) {
$(".result").html("Correct");
score += 1;
$(".score").html(score);
askQuestion(digits);
} else {
$(".result").html("Wrong");
score -= 1;
$(".score").html(score);
}
});
单击处理程序具有对旧答案值的引用。每次都必须从输入中获取值。大概是这样的:
// Rest of your code above
var answer = $(".answer");
console.log(correctanswer);
$(".question").html(factor1 + " × " + factor2);
var score = 0;
//Problem Starts Here
$(".check").click(function() {
// Don't check what is in the input until you're ready to use the value.
if (correctanswer == parseInt(answer.val(), 10)) {
$(".result").html("Correct");
score += 1;
$(".score").html(score);
askQuestion(digits);
} else {
$(".result").html("Wrong");
score -= 1;
$(".score").html(score);
}
});
$(".check").click(function() {
var answer = parseInt($(".answer").val(), 10);
if (correctanswer == answer) {
$(".result").html("Correct");
score += 1;
$(".score").html(score);
askQuestion(digits);
} else {
$(".result").html("Wrong");
score -= 1;
$(".score").html(score);
}
});
这是有效的,但我注意到的另一件事是,如果你第一次答对了一个问题,它会增加分数,但第二次不会。您知道如何解决此问题吗?每次输入askQuestion函数时,您的分数变量都设置为0。因此,他们永远不会得到超过1。您可以将该变量移到函数外部,也可以从$'.score'元素的值中读取。此外,还可以从addQuestion函数中获取check click事件。否则,每次调用该函数时,您都会添加另一个侦听器,您会看到分数以荒谬的速度上升。谢谢!我真的很感谢你的帮助!这是有效的,但我注意到的另一件事是,如果你第一次答对了一个问题,它会增加分数,但第二次不会。您知道如何解决此问题吗?每次输入askQuestion函数时,您的分数变量都设置为0。因此,他们永远不会得到超过1。您可以将该变量移到函数外部,也可以从$'.score'元素的值中读取。此外,还可以从addQuestion函数中获取check click事件。否则,每次调用该函数时,您都会添加另一个侦听器,您会看到分数以荒谬的速度上升。谢谢!我真的很感谢你的帮助!