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
Asked
Active
Viewed 8,655 times
0
-
I think you need some math CAPTCHA, so maybe this article [Javascript form validator conflict with math CAPTCHA](http://stackoverflow.com/questions/10726362/javascript-form-validator-conflict-with-math-captcha) or this [Implementation of Captcha in Javascript](http://www.codeproject.com/Articles/42842/Implementation-of-Captcha-in-Javascript) helps you. – Maryam Arshi Mar 10 '13 at 11:31
3 Answers
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");

Esailija
- 138,174
- 23
- 272
- 326
-
I think `questions.reduce(function(a,b){return a+b}, 0)` would look nicer :-) – Bergi Mar 10 '13 at 11:47
-
[Why?](http://stackoverflow.com/q/12961085/1048572) Do you want me to remove my upvote for that? :-) – Bergi Mar 10 '13 at 12:01
-
2
- Define possible operations in an array
- Take randomly one of these operations
- Take randomly two numbers between some limits (e.g.: 0-30) (check for unnacceptable cases like 10 / 0)
- 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;}
}

bgusach
- 14,527
- 14
- 51
- 68
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.

Phil H
- 19,928
- 7
- 68
- 105
-
-
@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? – Phil H Mar 10 '13 at 11:52
-
No, you seem not to have understood how to write constructors and how to use prototypes. – Bergi Mar 10 '13 at 11:56
-
@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. – Phil H Mar 10 '13 at 12:07
-
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. – Bergi Mar 10 '13 at 12:08
-
@Bergi: Yes, I'm a big fan of multiply :). I was aware, so I've fixed it now. – Phil H Mar 10 '13 at 12:11