当前答案正确时生成随机和方程

时间:2017-01-01 14:46:20

标签: javascript html

<body> 
 
<div class="container">
  <div class="center">
  <h1>Zeen Game</h1>
  <div class="panel panel-default">
    <div class="panel-body"><input type="text" id="oneA"> + <input type="text" id="oneB"> answer:<input type="text" id="ansone"><input type="button" value="check" onclick="checkAns('one')"/></div>
  </div>
</div>

<script type="text/javascript">
    document.getElementById("oneA").value=Math.floor(Math.random()*11+1);
    document.getElementById("oneB").value=Math.floor(Math.random()*11+1);

    function checkAns(grade){
        var answer = Number(document.getElementById(grade+"A").value) + 
                     Number(document.getElementById(grade+"B").value);
        if (answer==Number(document.getElementById("ans"+grade).value)) {
            alert("well done!");
        } else {
            alert("try again!");
        }
    }
</script>
</body>

我有两个随机数之和的功能“检查答案”。 我希望当前答案正确时,然后生成新的随机数,以制作新的和方程式。

1 个答案:

答案 0 :(得分:0)

您必须将脚本的第一行添加到if语句块中,并清除下一个等式的答案块, 像这样

&#13;
&#13;
<body> 
 
<div class="container">
  <div class="center">
  <h1>Zeen Game</h1>
  <div class="panel panel-default">
    <div class="panel-body"><input type="text" id="oneA"> + <input type="text" id="oneB"> answer:<input type="text" id="ansone"><input type="button" value="check" onclick="checkAns('one')"/></div>
  </div>
</div>

<script type="text/javascript">
    document.getElementById("oneA").value=Math.floor(Math.random()*11+1);
    document.getElementById("oneB").value=Math.floor(Math.random()*11+1);

    function checkAns(grade){
        var answer = Number(document.getElementById(grade+"A").value) + 
                     Number(document.getElementById(grade+"B").value);
        if (answer==Number(document.getElementById("ans"+grade).value)) {
            alert("well done!");
            document.getElementById("ans"+grade).value = '';
            document.getElementById("oneA").value=Math.floor(Math.random()*11+1);
            document.getElementById("oneB").value=Math.floor(Math.random()*11+1);
        } else {
            alert("try again!");
        }
    }
</script>
</body>
&#13;
&#13;
&#13;