Javascript 我如何使我的职能按计划运作?我的逻辑错了吗?(长)
对不起,这个模糊的/私人的问题。我很难弄明白为什么我的代码不能像我希望的那样工作。本质上,我的程序是一个数学游戏。我正在尝试做一个“重新测验”函数,让你重做你做错的数学题 所以我有一个叫做加法的函数:Javascript 我如何使我的职能按计划运作?我的逻辑错了吗?(长),javascript,jquery,function,Javascript,Jquery,Function,对不起,这个模糊的/私人的问题。我很难弄明白为什么我的代码不能像我希望的那样工作。本质上,我的程序是一个数学游戏。我正在尝试做一个“重新测验”函数,让你重做你做错的数学题 所以我有一个叫做加法的函数: function addition() { //addition function function generateNumber() { //generateNumber is a function that creates 2 numbers based on the user's in
function addition() { //addition function
function generateNumber() { //generateNumber is a function that creates 2 numbers based on the user's input from cookies
var n1= Math.floor(Math.random() * (cookiearray[2] - cookiearray[1]) + cookiearray[1]); //n1 is equal to a random number between the user input. It grabs values from the cookie array.
var n2= Math.floor(Math.random() * (cookiearray[2] - cookiearray[1]) + cookiearray[1]); //n2 is also a random number.
document.getElementById("question").innerHTML = n1 + " + " + n2 + "=" ; //this asks the user the question
window.answer = n1+n2;
} //end of generateNumber function
generateNumber(); //calls the generateNumber function.
}
此函数只生成2个随机数,并要求您将其相加。现在,为了让计算机知道答案,我让it将答案存储为一个称为“答案”的全局变量
然后,我使用jQuery来评估答案。如果按enter键,则检查答案是否与用户输入的内容相同。如果错误,则计算机将问题放入一个数组,并将该问题的答案放入另一个数组:
$("#input").keyup(function(event){ //targets the input box
if(event.keyCode == 13){ //when enter is pressed
if(answer == $('#input').val()){ //evaluate if the input value was equal to the global variable answer (from game.js)
//if it is the correct answer...
score += 1; //add score by 1
document.getElementById("score").innerHTML = "Score: " + score ; //print score
addition();
$('#input').val('') //clears text box for next question
}
else {
//if the input answer was incorrect...
requizQuestions.push(display1);
requizAnswers.push(answer);
document.getElementById("incorrect").innerHTML += "<br />" + "The answer to " + display1 + " was " + answer + " not " + $('#input').val();
addition();
$('#input').val('') //clears text box for next question
}
} //end if statement (with keycode)
我想我会采取稍微不同的方法。提前生成所有问题及其答案,并将其存储在一个数组中。然后,从数组中删除一个问题并将其显示给用户——如果用户回答不正确,则将该问题添加到列表的后面。一旦数组为空,测验就结束了。以下是一个工作示例:
var问题=[];
var=null;
最小值,最大值;
函数startName(){
最小值=数字(提示(“输入最小值”,0));
max=Number(提示(“输入最大值”,10));
设numberOfQuestions=Number(提示(“输入问题数量”,5));
//预先创建所有问题
for(设i=0;i0){
//从数组中找出下一个问题
question=questions.shift();
$(“#问题”).text(问题.问题);
}否则{
$(“问题”)。文本(“完成”);
}
}
函数generateNumber(){//generateNumber是一个基于用户从Cookie中输入的数据创建2个数字的函数
var n1=Math.floor(Math.random()*(max-min)+min);//n1等于用户输入之间的随机数。它从cookie数组中获取值。
var n2=Math.floor(Math.random()*(max-min)+min);//n2也是一个随机数。
var question=n1+“+”+n2+“=”;//这会向用户询问问题
var应答=n1+n2;
返回{
问题:问题:,
答复:答复:
};
}
$(函数(){
startGame();
$(“#答案”)。关于(“键控”,函数(e){
如果(如keyCode===13){
//获取用户输入的号码
让答案=数字(该值);
//如果错误,请添加到问题列表的末尾
如果(答案!==问题.答案)
问题。推(问题);
//继续下一个问题
askNext();
}
});
});代码>
function requiz() {
var i = 0;
window.answer = requizAnswers[i];
document.getElementById("question").innerHTML = requizQuestions[i];
$("#input").keyup(function(event){ //targets the input box
if(event.keyCode == 13){ //when enter is pressed
if(answer === $('#input').val()){ //evaluate if the input value was equal to the global variable answer (from game.js)
i+=1;
window.answer = requizAnswers[i];
document.getElementById("question").innerHTML = requizQuestions[i];
} else {
//didn't know what to put here
}
} //end if statement (with keycode)
});
}