我在下面为一个简单的数学游戏修改/编辑了代码。您输入要解决的金额,然后解决!
我现在遇到的问题是,在最后,它只显示分数。
我试图让它不仅显示分数,还显示您输入要解决的问题数量。因此,它更像是一个“你得到了”分数“超出”输入正确的问题。下面是代码和 Fiddle 的链接。
http://jsfiddle.net/justinw001/Mttw6/12/
$(function () {
var score = 0;
var questions = [];
$('#gen').click(function () {
score = 0;
questions = [];
var questionAmount = parseInt($('#inputElement').val(), 10);
for (var i = 0; i < questionAmount; i++) {
var q = {
x: Math.floor(Math.random() * 13),
y: Math.floor(Math.random() * 13)
};
questions.push(q);
}
nextQuest(questions.pop());
});
$('#sub').click(function () {
var ans, x, y;
if (questions.length >= 0) {
ans = parseInt($('#answer').val(), 10);
x = parseInt($('#input1').text(), 10);
y = parseInt($('#input2').text(), 10);
if (ans === x * y) {
score++;
}
nextQuest(questions.pop());
}
});
var nextQuest = function (q) {
if (q) {
$('#input1').text(q.x);
$('#input2').text(q.y);
$('#answer').val('');
$('#inputElement').val(questions.length);
} else {
$('#input1, #input2').text('');
$('#answer, #inputElement').val('');
alert(score);
}
};
});
最佳答案
在函数外声明questionAmount
$(function () {
var score = 0;
var questions = [];
var questionAmount=0; //here
$('#gen').click(function () {
score = 0;
questions = [];
questionAmount = parseInt($('#inputElement').val(), 10);
然后当你提醒时这样做
alert(score+" out of "+questionAmount + " correct");
关于javascript - 尝试创建一个数学游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21434387/