Javascript.如何将问题和正确答案返回给 Math.random

Javascript. How to return the questions and correct answers to a Math.random

本文关键字:返回 答案 Math random 问题 Javascript      更新时间:2023-09-26

我对javascript相当陌生,遇到了一个问题。我找到了这个代码片段,并尝试在用户输入所有问题的答案后显示问题、用户答案和 ask() 的正确答案。我尝试了一个 for 循环来显示每个索引,但这只返回 true 或 false。

代码如下:

<script>
    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");
</script>

您可以在此处测试当前代码

请参阅 http://jsfiddle.net/on1nnzpj/1/

ask()返回一个函数,然后将给出的答案与正确答案进行比较:

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 new function() { this.op = op; this.ans = prompt("How much is " + a + " " + op + " " + b + "?"); this.cor = eval( a + op + b); this.suc = this.ans == this.cor};
}
function isCorrect(element){
    return element.suc;
}
var questions = [ask(), ask(), ask(), ask(), ask()],
    total = questions.length;
    console.log(questions); 
    var correct = questions.filter(isCorrect).length;

alert( "You got "+correct+"/"+total+" correctly");
function alertQ(elem){
    if (!elem.suc){
        alert ("Your answer was: " + elem.ans + " correct answer was: " + elem.cor);
    }
}
questions.forEach(alertQ);

更新

改变

return new function() { this.op = op; this.ans = prompt("How much is " + a + " " + op + " " + b + "?"); this.cor = eval( a + op + b); this.suc = this.ans == this.cor};

为此添加问题:

return new function() { this.op = op; this.q = "How much is " + a + " " + op + " " + b + "?"; this.ans = prompt(this.q); this.cor = eval( a + op + b); this.suc = this.ans == this.cor};

并修改警报Q:

您对问题 1:5 *4 的回答是 20。没错!

function alertQ(elem, index){
    var c = elem.cor ? " That was correct!" : " That was incorrect!"
    alert ("Your answer for question " + (index + 1) + ": " + elem.q + " was: " + elem.ans + c);
}

您可以使用对象从 ask() 函数返回。

JSFIDDLE 演示

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 {
            question : "How much is " + a + " " + op + " " + b + "?",
            ansGiven: prompt("How much is " + a + " " + op + " " + b + "?"),
            ansCorrect : eval( a + op + b),
            get isCorrect(){return this.ansGiven == this.ansCorrect}
           };
}
var questions = [ask(), ask(), ask(), ask(), ask()],
    total = questions.length,
    correct = questions.filter(function(ans){return ans.isCorrect}).length;
alert( "You got "+correct+"/"+total+" correctly");
questions.forEach(function(q, i){
    alert("Question " +(i+1)+ " : " + q.question 
    + "'nAnswer: " + q.ansGiven 
    + "'nCorrect Answer: " + q.ansCorrect 
    + "'n You were " + ((q.isCorrect)?"Correct!":"Wrong!"));
});