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.
Aucun commentaire:
Enregistrer un commentaire