0

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);
 };
vkumar
  • 863
  • 4
  • 9
  • 14
  • 2
    You can use `eval` if you really want to store operators as strings. A better approach might be to create an array of functions and choose a function (rather than an operator) at random. – fzzfzzfzz May 22 '15 at 01:00
  • What exactly is "not working"? Any error at your console? – NemoStein May 22 '15 at 01:10

1 Answers1

2

You can use eval:

var answer = eval(x + operators[operatorChoice] + y);

eval is usually frowned upon, because it can be dangerous if the argument includes user input. But since you're generating all the input yourself, and it only contains numbers and operators from your list, this is a reasonable use of it.

A better, and more general way to handle this, would be to define functions for each operation, and call them.

var mathGame = function() {
    function add(x, y) { return x + y; }
    function subtract(x, y) { return x - y; }
    function multiply(x, y) { return x * y; }
    function divide(x, y) { return x / y; }
    var operators = ["+", "-", "*", "/"];
    var operations = [add, subtract, multiply, divide];
    var operatorChoice = Math.floor(Math.random() * operators.length); 
    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 = operations[operatorChoice](x, 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);
};
mathGame();

Yuo also had another bug in your code. You shouldn't add 1 to the random number when you're setting operatorChoice. That produces a number from 1 to 4, but the indexes of operators are 0 to 3.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This is a correct answer, it answers the exact question asked, and it is given with a warning that `eval` is considered bad practice. `eval` is not a crime, it's just not a good tool in production code. – fzzfzzfzz May 22 '15 at 01:14
  • @user3184082 http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – Barmar May 22 '15 at 01:20
  • @NemoStein I've added more showing how to do it with functions. – Barmar May 22 '15 at 01:20
  • Have a look at [this](http://stackoverflow.com/a/12961206/1048572) for better ideas – Bergi May 22 '15 at 01:57
  • @Barmar, nice addition! It's, indeed, a way better approach... (ps: that -1 wasn't mine... ^^") – NemoStein May 22 '15 at 16:41