0

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

1

3 Answers 3

3
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");
Sign up to request clarification or add additional context in comments.

3 Comments

I think questions.reduce(function(a,b){return a+b}, 0) would look nicer :-)
Why? Do you want me to remove my upvote for that? :-)
Yes, well the eval is safe here though xD
2
  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;}
}

Comments

0

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.

6 Comments

Why do you use new? What is q.checkResult?
@Bergi: question is now an object prototype, so q = new question() should yield a new question with new a and b values. You can check a result for this question using q.checkResult with the supplied string. From your reputation I think you know that, so what are you pointing out?
No, you seem not to have understood how to write constructors and how to use prototypes.
@Bergi: Thanks for your input, I've modified the answer. Too much time in C++ recently. I'd have got there quicker with some hint in the comment, but the point is valid.
Thanks, better now. Still, you could use the prototype :-) Btw, your odd combination of Math.min and Math.round for Math.random leads to a very biased operator choice.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.