生成随机数随机方程

时间:2016-05-06 22:39:19

标签: javascript html random

我想在JavaScriptp中生成随机方程式,然后将它们输出到HTML标记中。 这是代码:

<!DOCTYPE html>
<body>

<p>Click the button below to generate a random equation.</p>

<button onclick="change();">Generate</button>

<p id="generate"></p>

<script>
function getRandomizer(bottom, top) {
    return function() {
        return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
    }
}

function getRandomNumber(results) {
    var rollDie = getRandomizer( 1, 10 );
    for ( var i = 0; i < 3; i++ ) {
         results += rollDie() + "";
    }
    getRandomNumber.result = results;
}

function getRandomEquation(num1, num2, num3, num4, num5, num6, num7, output) {
    var num_7,
        num_6,
        num_5,
        num_4,
        num_3,
        num_2,
        num_1
     getRandomNumber(num1).result = num_7;
     getRandomNumber(num2).result = num_6;
     getRandomNumber(num3).result = num_5;
     getRandomNumber(num4).result = num_4;
     getRandomNumber(num5).result = num_3;
     getRandomNumber(num6).result = num_2;
     getRandomNumber(num7).result = num_1;

     var equation1 = "" + num_1 + " x " + num_2 + " + {" + num_3 + " x [(" + num_4 + " x " + num_5 + ") - " + num_6 + "] + " + num_7 + "} = x",
         equation2 = "" + num_1 + " x " + num_2 + " = y",
         equation3 = "" + num_1 + "s x " + num_2 + " = z, s = " + num_3,
         equation4 = "" + num_1 + " + {" + num_2 + " x [" + num_3 + " + (" + num_4 + " x " + num_5 + ") + " + num_6 + "] + " + num_7 + "} = x",
         equation5 = "" + num_1 + "e + " + num_2 + "l x " + num_3 + " + " + num_4 + "a, e = " + num_5 + ", l = " + num_6 + ", a = " + num_7,
         equation6 = "[" + num_1 + " x " + num_2 + "z] + {" + num_3 + " - " + num_4 + "} + (" + num_5 + " + " + num_6 + ") = e, z = " + num_7,
         equation7 = "p" + " x " + num_1 + " / " + num_2 + " - " + num_3 + " + " + num_4 + " = e, p = " + num_5
    var values = [
         // there is an easier way to do this, too lazy
         "" + equation1,
         "" + equation2,
         "" + equation3,
         "" + equation4,
         "" + equation5,
         "" + equation6,
         "" + equation7
    ]
    var i = 0;
    var e;
    if (i > values.length) {
         i = 0;
    }
    var randomEquation = values[i];
    i++;
    e = values[i];


    this.output = randomEquation;
    this.e = e;
}

function getEquation() {
    var bl1,
        bl2,
        bl3,
        bl4,
        bl5,
        bl6,
        bl7,
        equationOutput;

        var eq = getRandomEquation(bl1, bl2, bl3, bl4, bl5, bl6, bl7, equationOutput).e;

        getEquation.equation = eq;
    }
    function change() {
        var final = getEquation().equation;
        document.getElementById("generate").innerHTML = final;
    }
</script>

</body>
</html>

但它不起作用。有什么帮助吗?

P.S。我的老师把这个分配给了我。请尽快回复。感谢。

2 个答案:

答案 0 :(得分:0)

嗯,你可以这样做:

var type = (Math.floor(Math.random(4));
var ret = 0;
var n = [(Math.floor(Math.random(100)), (Math.floor(Math.random(100))];
if (type == 0) ret = n[0] + n[1]
else if (type == 1) ret = Math.abs(n[0] - n[1])
else if (type == 2) ret = n[0] * n[1];
else if (type == 3) ret = n[0] / n[1]
else ret = n[0] / 5 % n[1]
// do something with ret

它完全可扩展,只需修改nif语句

答案 1 :(得分:0)

这段代码完全混乱。我不知道它来自哪里,但绝对不是Javascript。 请尝试以下方法:

<!DOCTYPE html>
<body>

<p>Click the button below to generate a random equation.</p>

<button onclick="change();">Generate</button>

<p id="generate"></p>

<script>
function getRandomizer(bottom, top) {
    return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}

function getRandomNumber() {
    var results="";
    for ( var i = 0; i < 3; i++ ) {
         results += getRandomizer( 1, 10 );
    }
    return results;
}

function getRandomEquation() {
    var num_7 = getRandomNumber(),
        num_6 = getRandomNumber(),
        num_5 = getRandomNumber(),
        num_4 = getRandomNumber(),
        num_3 = getRandomNumber(),
        num_2 = getRandomNumber(),
        num_1 = getRandomNumber();

    var equation1 = num_1+" x "+num_2+" + {"+num_3+" x [("+num_4+" x "+num_5+") - "+num_6+"] + "+num_7+"} = x",
        equation2 = num_1+" x "+num_2+" = y",
        equation3 = num_1+"s x "+num_2+" = z, s = "+num_3,
        equation4 = num_1+" + {" +num_2+ " x [" +num_3+" + ("+num_4+" x "+num_5+") + "+num_6+"] + "+num_7+"} = x",
        equation5 = num_1+"e + "+num_2+"l x "+num_3+" + "+num_4+"a, e = "+num_5+", l = "+num_6+", a = "+ num_7,
        equation6 = "["+num_1+" x "+num_2+ "z] + {"+num_3+" - "+num_4+"} + ("+num_5+" + "+num_6+") = e, z = "+ num_7,
        equation7 = "p x "+num_1+" / "+num_2+" - "+num_3+" + "+num_4+" = e, p = "+num_5
    var randomEquation = [
         equation1,
         equation2,
         equation3,
         equation4,
         equation5,
         equation6,
         equation7
    ]


    return randomEquation.join("<br>");
}

function change() {
    document.getElementById("generate").innerHTML = getRandomEquation();
}
</script>

</body>
</html>