的JavaScript。如何返回问题并更正Math.random的答案

时间:2014-11-20 03:36:55

标签: javascript arrays

我对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>

您可以测试当前代码here

2 个答案:

答案 0 :(得分:1)

请参阅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};

并修改alertQ:

你对问题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);
}

答案 1 :(得分:0)

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

JSFIDDLE DEMO

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!"));
});