Mac Mac - 1 month ago 9
Javascript Question

Solving random numbers and random variables from array Javascript

window.onload = start;
var nr1 = Math.floor(Math.random()*50);
var nr2 = Math.floor(Math.random()*50);
var eq1 = nr1 +"+"+nr2;
var eq2 = nr1 + "-"+ nr2;
var eq3 = nr1 +"*"+ nr2;
var eq4 = nr1 +"/"+ nr2;

var arr = [eq1,eq2,eq3,eq4];
var eq = arr[Math.floor(Math.random()*arr.length),Math.floor(Math.random()*arr.length)];


I want to practice something, making a basic Math game. Up there is the code I'm trying to use to generate random numbers and random equations which I want to come out of my html. The reason why I have "" for the operators is because I want the whole equation to come out as a text, I've tried it without the "" marks and I just get the answer straight away.

function start() {
document.getElementById("submit").onclick = submitans;
document.getElementById("question").innerHTML = eq;
}


this is my startup function which generates the equation. Now this works very well in the html if you look at this photo --
HTML. My problem is, I don't know how I'm going to connect the equation to the "if(statement)" from the input box. Because the script reads the commands as text, so it doesn't work even if I write

function submitans() {

var correct = "Correct!";
var wrong = "Wrong";
var inn = parseInt(document.getElementById("answer").value);

for (var i = 0; i < eq.length; i++) {
if(inn === eq[i])
document.getElementById("input").innerHTML = "correct";
}
}


Is there in anyway I can reach the goal I want from this script? I'm sticking with pure javascript, as I am just a beginner, and I really want to learn it. So I'd rather not use jquery.

Aside from this, I'm planning to make my script show the score for every right answer, I thought of having a maximum of 10 right answers for the goal, and the user is allowed to have 5 mistakes.

Answer

in the first snippet of code you can add the answer for each equation

var eq1 = nr1 +"+"+nr2;
var eq2 = nr1 + "-"+ nr2;
var eq3 = nr1 +"*"+ nr2;
var eq4 = nr1 +"/"+ nr2;
//answers
var ans1 = nr1 +"+"+nr2;
var ans2 = nr1 + "-"+ nr2;
var ans3 = nr1 +"*"+ nr2;
var ans4 = nr1 +"/"+ nr2;

and then when you get the random equation you get the answer for it too

var arr = [eq1,eq2,eq3,eq4];
var ansArr = [ans1, ans2, ans3, ans4];
var eqNumber = Math.floor(Math.random()*arr.length);
var eq = arr[eqNumber];
var ans = ansArr[eqNumber];

and edit the last snippet like this

function submitans() {
    var correct = "Correct!";
    var wrong = "Wrong";      
    var inn = parseInt(document.getElementById("answer").value);

    //for (var i = 0; i < eq.length; i++) {//delete
       if(inn == ans)
        document.getElementById("input").innerHTML = correct;
       else
        document.getElementById("input").innerHTML = wrong;
    //}//delete
 }

--------------------------------------------------------------------------------

or instead of all of that you can also only change the last snippet of code like this

function submitans() {
    var correct = "Correct!";
    var wrong = "Wrong";      
    var inn = parseInt(document.getElementById("answer").value);

    //for (var i = 0; i < eq.length; i++) {//delete
       if(inn == eval(eq))
        document.getElementById("input").innerHTML = correct;
       else
        document.getElementById("input").innerHTML = wrong;
    //}//delete
 }

eval will run the text as javascript and it will return the answer of the equation.