vendredi 6 mai 2016

Generate random equations with random numbers

I want to generate random equations in JavaScriptp and then output them into an HTML tag. This is the code:

<!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>

But it dosen't work. Any help?

P.S. My teacher assigned this to me. Please respond as soon as possible. Thanks.




Aucun commentaire:

Enregistrer un commentaire