Sky Wang
Sky Wang

Reputation: 13

How to use Javascript to ask the user random math questions?

How to use Javascript to ask the user random math questions? If the user answers correctly, the program is suppossed to say Correct! If the answer is wrong, the program is suppossed to say FALSE! I'm thinking of using if and else statements, but I just don't know how. Also, i'm thinking of making the program ask different random number ADDITION questions to the user 5 times. AT the end, the program gives the user a rating, such as 4/5 questions answered correctly! Random number range: 1-10

Upvotes: 0

Views: 9206

Answers (3)

bgusach
bgusach

Reputation: 15175

  1. Define possible operations in an array
  2. Take randomly one of these operations
  3. Take randomly two numbers between some limits (e.g.: 0-30) (check for unnacceptable cases like 10 / 0)
  4. Compare user input with the computed result. If float, apply a small tolerance.

The implementation couldn't be easier.

EDIT Hint: construct an object that contains all the functions and take randomly from it. This way you avoid eval():

var operations = {
    '+': function (a, b) {return a + b;},
    '-': function (a, b) {return a - b;},
    '/': function (a, b) {return a / b;},
    'x': function (a, b) {return a * b;}
}

Upvotes: 2

Phil H
Phil H

Reputation: 20141

If you're happy with just addition:

function question() {
    this.a = Math.round(Math.random()*10);
    this.b = Math.round(Math.random()*10);
    this.result = this.a + this.b;
    this.checkResult = function(givenResultString) {
        return (""+result == givenResultString);
    }
}

Then you can create a question each time with:

var q = new question();

And check an answer:

var response = q.checkResult(someString) ? "Correct!" : "FALSE!";

The rest of the job is the mechanics of the page, for which you could use a form and a result div.

If you want to add more operations, you would pick a random operator as well as random inputs:

function question() {
    var add(x, y) { return x+y; };
    var subtract(x, y) { return x-y; };
    var multiply(x, y) { return x*y };
    var operators = [add, subtract, multiply];

    this.a = Math.round(Math.random()*10);
    this.b = Math.round(Math.random()*10);
    var operatorIdx = Math.min(Math.floor(Math.random()*4),3);
    this.operator = operators[operatorIdx];
    this.result = operator(this.a,this.b);
    this.checkResult = function(givenResultString) {
        return (""+this.result == givenResultString);
    }
}

I've left division off here, as the rest assumes integers and you'd have to change your initialisation to prevent a division from producing fractional values. The straightforward way would be to initialise a multiply, then swap the result and a.

Upvotes: 0

Esailija
Esailija

Reputation: 140228

function ask() {
    var a = Math.floor(Math.random() * 10) + 1;
    var b = Math.floor(Math.random() * 10) + 1;
    var op = ["*", "+", "/", "-"][Math.floor(Math.random()*4)];
    return prompt("How much is " + a + " " + op + " " + b + "?") == eval( a + op + b);
}

var questions = [ask(), ask(), ask(), ask(), ask()],
    total = questions.length,
    correct = questions.filter(Boolean).length;

alert( "You got "+correct+"/"+total+" correctly");

Upvotes: 3

Related Questions