- 我创建了两个函数(
F1
和F2
) F1
创建随机答案F2
应该对F1
生成的答案提供反馈 (if(antw == ant
)- 我的问题是如何使
F2
与随机答案F1
配合使用 生成的反馈是否正确?
var operators = ['+','-'];
function F1()
{
Z1 = document.getElementById("Z1");
Z2 = document.getElementById("Z2");
oper=document.getElementById("operator");
answer=document.getElementById("answer");
rZ1 = Math.floor((Math.random()*10));
rZ2 = Math.floor((Math.random()*10)+1);
op = operators[Math.floor(Math.random()*2)];
Z1.innerHTML=rZ1;
Z2.innerHTML=rZ2;
oper.innerHTML=op;
answer.innerHTML = eval(rZ1 + op + rZ2);
}
document.getElementById("button2").addEventListener("click", F2);
function F2()
{
antw = parseInt(document.getElementById("userAnswer").value, 10);
feedBack = document.getElementById("feedBack");
ant = document.getElementById("answer");
{
if(antw == ant) {
feedBack.textContent = "right";
} else {
feedBack.textContent = "wrong";
}
}
};
<button onclick="F1()"> New </button>
<p>
<label id="Z1"> </label>
<label id="operator"> </label>
<label id="Z2"> </label>
= <input id = "userAnswer" type=text>
<button id = "button2" >answer</button>
</p>
<p id = "feedBack"> </p>
<p><label id="answer"> </label></p>
最佳答案
您可以在函数 F1 的末尾调用 F2,并将结果作为参数传递给 F2。
function F2(answer){
//logic to check answer
return feedback
}
function F1(){
//logic to calculate answer
F2(answer)
}
您可以使用原来的代码,只需要修复代码中的一个小问题。您忘记使用 textContent 从 html 中获取值,因此您将未定义的值与您的答案进行比较
ant = document.getElementById("answer").textContent;
关于javascript - 如何创建一个对 Javascript 中先前函数生成的内容使用react的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60731844/