Javascript 如何检查用户';s的输入与数学函数结果匹配?
我正在制作一个游戏,在网页上为用户创建随机数学问题。我需要检查用户的输入(他们的答案)是否与随机函数的结果相匹配。我有四个函数,当点击“新问题”按钮时,它们被随机调用。我想通过在页面上显示“正确”或“不正确”来查看用户的输入是否与函数结果匹配。有人有什么想法吗?我对JS还相当陌生,我不确定谷歌会为这一个做些什么,但我愿意查一下。有一些方向Javascript 如何检查用户';s的输入与数学函数结果匹配?,javascript,html,Javascript,Html,我正在制作一个游戏,在网页上为用户创建随机数学问题。我需要检查用户的输入(他们的答案)是否与随机函数的结果相匹配。我有四个函数,当点击“新问题”按钮时,它们被随机调用。我想通过在页面上显示“正确”或“不正确”来查看用户的输入是否与函数结果匹配。有人有什么想法吗?我对JS还相当陌生,我不确定谷歌会为这一个做些什么,但我愿意查一下。有一些方向 const btn=document.querySelector(“#start”) 常数randomFunc=[ 乘法, 分部, 附加 减法, ] btn
const btn=document.querySelector(“#start”)
常数randomFunc=[
乘法,
分部,
附加
减法,
]
btn.addEventListener(
“点击”,
函数(){
randomFunc[Math.floor(Math.random()*randomFunc.length)]();
}
)
函数乘法(){
设num1=Math.floor(Math.random()*13);
设num2=Math.floor(Math.random()*13);
让problemResult=num1*num2;
log(num1,'*',num2,'=',problemResult);
document.getElementById('mathProblem').innerHTML=
(`${num1}*${num2}=`);
}
职能司(){
设num1=Math.floor(Math.random()*13);
设num2=Math.floor(Math.random()*12)+1;
让problemResult=(num1*num2)/num2;
log(num1*num2'/',num2'=',problemResult);
document.getElementById('mathProblem').innerHTML=
(`${num1*num2}/${num2}=`);
}
函数加法(){
设num1=Math.floor(Math.random()*13);
设num2=Math.floor(Math.random()*13);
让problemResult=num1+num2;
log(num1,“+”,num2,“=”,problemResult);
document.getElementById('mathProblem').innerHTML=
(`${num1}+${num2}=`);
}
函数减法(){
设num1=Math.floor(Math.random()*13);
设num2=Math.floor(Math.random()*13);
设numList=[num1,num2];
numList.sort(函数(a,b){
返回a-b
});
让problemResult=numList[1]-numList[0];
log(numList[1]、'-',numList[0]、'=',problemResult);
document.getElementById('mathProblem').innerHTML=
(`${numList[1]}-${numList[0]}=`);
}
新问题
您的问题将出现在此处
如果全局声明“problemResult”变量,并从其他声明中删除“let”,则可以在其他位置访问该值。在本例中,我添加了一个用于检查答案的按钮:
const btn=document.querySelector(“#start”)
常数randomFunc=[
乘法,
分部,
附加
减法,
]
var问题结果//全球宣布
btn.addEventListener(
“点击”,
函数(){
randomFunc[Math.floor(Math.random()*randomFunc.length)]();
}
)
函数乘法(){
设num1=Math.floor(Math.random()*13);
设num2=Math.floor(Math.random()*13);
problemResult=num1*num2;/“let”修剪
log(num1,'*',num2,'=',problemResult);
document.getElementById('mathProblem').innerHTML=
(`${num1}*${num2}=`);
}
职能司(){
设num1=Math.floor(Math.random()*13);
设num2=Math.floor(Math.random()*12)+1;
problemResult=(num1*num2)/num2;/“let”修剪
log(num1*num2'/',num2'=',problemResult);
document.getElementById('mathProblem').innerHTML=
(`${num1*num2}/${num2}=`);
}
函数加法(){
设num1=Math.floor(Math.random()*13);
设num2=Math.floor(Math.random()*13);
problemResult=num1+num2;/“let”修剪
log(num1,“+”,num2,“=”,problemResult);
document.getElementById('mathProblem').innerHTML=
(`${num1}+${num2}=`);
}
函数减法(){
设num1=Math.floor(Math.random()*13);
设num2=Math.floor(Math.random()*13);
设numList=[num1,num2];
numList.sort(函数(a,b){
返回a-b
});
problemResult=numList[1]-numList[0];/“let”修剪
log(numList[1]、'-',numList[0]、'=',problemResult);
document.getElementById('mathProblem').innerHTML=
(`${numList[1]}-${numList[0]}=`);
}
新问题
您的问题将出现在此处
检查
您需要让函数返回problemResult
,存储它,然后在输入时将它与用户输入进行比较
目前,您根本没有在监听用户输入。您可以通过“提交”按钮(即,用户完成操作后需要单击按钮)来完成此操作,或者我们可以在用户输入数据时进行监听。让我们做后者:
btn.addEventListener('click', () => {
let result = randomFunc[Math.floor(Math.random() * randomFunc.length)]();
document.querySelector('#user_input').addEventListener('input', evt => {
if (result.toString() === evt.target.value) {
/* correct! do something here */
} else {
/* not correct yet... */
}
});
})
请注意我们是如何将result
转换为字符串的,因此我们将苹果与苹果以及用户输入(来自字段值的输入将始终是字符串)进行比较
不要忘记调整函数以返回
problemResult
您为用户输入答案的元素创建了一个类或div。创建一个存储用户输入值的变量,然后使用if()
语句显示所需结果:
var input= document.getElementById("user_input");
if(input== problemresult){
alert(correct);
} else {
alert(incorrect);
}
让函数返回
problemResult
,当用户提交答案时,比较该值。注意:您首先需要协调它们的类型-即两者都是字符串或两者都是整数。@Mitya完全正确!我想将problemResult
与用户在输入框中键入的内容进行比较。我只是不知道如何将输入框连接到problemResult
。我认为最好将它们都设为整数,这样,如果用户输入除整数以外的任何内容,都将无效。这将导致错误。OP脚本很可能(或至少应该)在闭包中运行,这意味着problemResult
仍然是局部变量,而不是真正的全局变量。此外,真正的全局变量是一个非常糟糕的主意。@Lemondoge谢谢!我