如何创建一个函数来响应 Javascript 中先前函数生成的内容

How to create a function that reacts to what a previous function generated in Javascript

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;