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
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
Share Improve this question asked Mar 10, 2013 at 11:28 Sky WangSky Wang 131 silver badge4 bronze badges 1- I think you need some math CAPTCHA, so maybe this article Javascript form validator conflict with math CAPTCHA or this Implementation of Captcha in Javascript helps you. – Maryam Arshi Commented Mar 10, 2013 at 11:31
3 Answers
Reset to default 3function 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");
- 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 puted 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;}
}
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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744311946a4567990.html
评论列表(0条)