.innerHTML不改变数学测验的价值

时间:2017-03-31 09:49:30

标签: javascript html css

作为学校项目的一部分,我必须使用HTML,CSS,Javascript和JQuery进行完整的随机数学测验(有三个操作员),如果必要的话。它还必须包括CSS样式,即它也必须看起来很好。

我遇到了一个路障,我的问题没有出现在指定的“问题”栏目中。我知道我犯了很多错误,这是我第一次使用javascript,任何帮助都会得到满足,如果你需要完整的代码,请问。感谢。

function getNumber(){
    var oplist = ["=","-","*"];
    num1 = Math.floor((Math.random() * 10) + 1);
    num2 = Math.floor((Math.random() * 10) + 1);
    op = oplist [Math.floor(Math.random()* oplist.length)];
    if (op == "+"){
        ans = (num1 + num2)
    }
    else if (op == "-"){
        ans = (num1 - num2)
    }
    else if (op == "*"){
        ans = (num1 * num2)
    }
}

document.getElementById("questions").innerHTML = num1 + op + num2;

4 个答案:

答案 0 :(得分:2)

在函数return num1 + op + num2+'='+ans;

中应用



var num1,num2,op
var ans =0;
function getNumber(){
    var oplist = ["=","-","*"];
    num1 = Math.floor((Math.random() * 10) + 1);
    num2 = Math.floor((Math.random() * 10) + 1);
    op = oplist [Math.floor(Math.random()* oplist.length)];
    if (op == "+"){
        ans = (num1 + num2)
    }
    else if (op == "-"){
        ans = (num1 - num2)
    }
    else if (op == "*"){
        ans = (num1 * num2)
    }
    return  num1 + op + num2+'='+ans;
}

document.getElementById("questions").innerHTML = getNumber();

<p id="questions"></p>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="https://code.jquery.com/jquery-3.2.1.js"
            integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
            crossorigin="anonymous"></script>
</head>
<body>
<div id="questions"></div>
    <div>
        <input type="button" onclick="getNumber()" />
    </div>
</body>
</html>

<script>

        function getNumber() {
            var oplist = ["=", "-", "*"];
            num1 = Math.floor((Math.random() * 10) + 1);
            num2 = Math.floor((Math.random() * 10) + 1);
            op = oplist[Math.floor(Math.random() * oplist.length)];
            if (op == "+") {
                ans = (num1 + num2)
            }
            else if (op == "-") {
                ans = (num1 - num2)
            }
            else if (op == "*") {
                ans = (num1 * num2)
            }

            document.getElementById("questions").innerHTML = num1 + op + num2;
        }
</script>

答案 2 :(得分:0)

你没有打电话给你的功能

document.getElementById(&#34; questions&#34;)。innerHTML = getNumber();

&#13;
&#13;
function getNumber(){
    var oplist = ["=","-","*"],
    num1 = Math.floor((Math.random() * 10) + 1),
    num2 = Math.floor((Math.random() * 10) + 1),
    op = oplist [Math.floor(Math.random()* oplist.length)],
ans = 0;
    if (op == "+"){
        ans = (num1 + num2)
    }
    else if (op == "-"){
        ans = (num1 - num2)
    }
    else if (op == "*"){
        ans = (num1 * num2)
    }
return ans;
}

document.getElementById("questions").innerHTML = getNumber();
&#13;
<div id="questions"></div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您的代码应该是这样的。

<html>
<head>
    <meta charset="utf-8" />
</head>
<body>
<div id="questions"></div>
    <div>
        <input type="button" onclick="getNumber()" value="Get Answer"/>
    </div>
<script>

        function getNumber() {
            var oplist = ["=", "-", "*"];
            num1 = Math.floor((Math.random() * 10) + 1);
            num2 = Math.floor((Math.random() * 10) + 1);
            op = oplist[Math.floor(Math.random() * oplist.length)];
            if (op == "+") {
                ans = (num1 + num2)
            }
            else if (op == "-") {
                ans = (num1 - num2)
            }
            else if (op == "*") {
                ans = (num1 * num2)
            }

            document.getElementById("questions").innerHTML = num1 + op + num2;
        }
</script>
</body>
</html>