简体   繁体   English

如何使用Javascript向用户随机提问数学问题?

[英]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

  1. Define possible operations in an array 在数组中定义可能的操作
  2. Take randomly one of these operations 随机采取这些操作之一
  3. Take randomly two numbers between some limits (eg: 0-30) (check for unnacceptable cases like 10 / 0) 在某些限制之间随机抽取两个数字(例如:0-30)(检查不可接受的情况,例如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(): 这样可以避免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.

相关问题 的JavaScript。 如何返回问题并更正Math.random的答案 - Javascript. How to return the questions and correct answers to a Math.random 使用LOOPS根据Javascript中的用户输入生成任意数量的随机数学问题 - Generating any # of random math questions based on user input in Javascript using LOOPS 如何使用 Math.random() 函数来随机化一系列测试问题? - How can I use the Math.random() function to randomize a series of test questions? 如何在 HTML p 标签中使用 JavaScript Math.random() - How to use JavaScript Math.random() within HTML p tag Javascript 中的简单关卡设计:如何询问用户是否要在第 N 个问题后继续游戏? - Simple level design in Javascript: How to ask user if they want to continue the game after Nth questions? JavaScript的Math.random是多么随机? - How random is JavaScript's Math.random? 如何使用JavaScript询问用户输入并将其分配给数组的值 - How to use JavaScript to ask input from User and assign it to values of arrays 是否可以使用 2 个提示来询问用户在 JavaScript 中的数据 - Is it possible to use 2 prompt that ask a user for data in JavaScript 如何在JavaScript中使用Math.random实现随机性? - How is randomness achieved with Math.random in javascript? Math.random() 如何在 javascript 中工作? - How does Math.random() work in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM