[英]How to use Javascript to ask the user random math questions?
How to use Javascript to ask the user random math questions? 如何使用Javascript向用户随机提问数学问题? If the user answers correctly, the program is suppossed to say Correct! 如果用户回答正确,则该程序会说“正确!”。 If the answer is wrong, the program is suppossed to say FALSE! 如果答案是错误的,则该程序被假定为FALSE! I'm thinking of using if and else statements, but I just don't know how. 我正在考虑使用if和else语句,但我只是不知道如何。 Also, i'm thinking of making the program ask different random number ADDITION questions to the user 5 times. 另外,我正在考虑使程序向用户询问5次不同的随机数ADDITION问题。 AT the end, the program gives the user a rating, such as 4/5 questions answered correctly! 最后,该程序给用户打分,例如正确回答了4/5个问题! Random number range: 1-10 随机数范围:1-10
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(): 这样可以避免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;}
}
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");
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. 剩下的工作就是页面的机制,您可以使用表单和结果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. 直接的方法是初始化一个乘法,然后交换结果和a。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.