I'm trying to make a math game in JQuery/JavaScript.
I have managed to create a code that comes up with a random X+ X+ =? math question. You have to write an answer in the box and then the code checks if the answer is right or wrong.
I want to use the same method but not just +, but -, * and / as well. I tried to change this in the code but it didn't work. Maybe someone can help me?
Here is the code:
$(function() {
getQuestion();
});
var count = 0,
results = [];
function getQuestion() {
count++;
var container = $('<div />');
var val1 = Math.round(Math.random()*5);
var val2 = Math.round(Math.random()*5);
var lbl = $('<label />');
lbl.html(val1 + ' + ' + val2 + ' = ');
container.append(lbl);
var input = $('<input type="text" />');
container.append(input);
var btn = $('<input type="button" value="SVARA" />');
var val;
btn.click(function() {
results.push({
number1 : val1,
number2 : val2,
answer : input.val()
});
input.attr('disabled', true);
$(this).attr('disabled', true);
$(this).after(function() {
if(val1 + val2 == input.val()) return 'RÄTT!';
return 'FEL';
});
getQuestion();
});
container.append(btn);
$('body').append(container);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>