I decided to make a math game in JavaScript, and I am very new to JS. I want it to choose a random operation from an array and evaluate it with two random numbers, but it is not working. Any help is appreciated. Thanks!
CODE:
var mathGame = function() {
var operators = ["+", "-", "*", "/"];
var operatorChoice = Math.floor((Math.random() * 4) + 1);
var points = 1;
var numberOfQuestions = prompt("How many questions would you like?");
var highestNumber = prompt("What is the highest number you would like to be quizzed on?");
for (var i = 0; i < numberOfQuestions; i++) {
var x = Math.floor((Math.random() * highestNumber) + 1);
var y = Math.floor((Math.random() * highestNumber) + 1);
var answer = (x operators[operatorChoice] y);
var user_answer = prompt(x + operators[operatorChoice] + y);
if (user_answer == answer) {
alert("Yes!");
points = points + 2;
} else {
if (points > 0) {
points = points - 2;
}
alert("Sorry, that was incorrect, the answer is " + answer);
}
}
alert("Your total points were " + points);
};