即使似乎都没有出现“语法错误”

时间:2019-06-24 11:27:44

标签: javascript

我正在编写一个生成随机数学测验的代码。但是,当我运行它(在一个名为SoloLearn的应用中)时,即使没有出现任何错误,也会出现语法错误。我什至在JSHint中测试了它,似乎没有语法错误。这是我的代码:

function random(max) {
    return Math.floor(Math.random() * Math.floor(max));
}
function start() {
    var form = document.getElementById("name_form");
    var name = form.elements[0].value;
    var score = 0;
    alert("Let's begin!");
    for (let i = 0; i < 4; i++) {
        var num1 = random(10);
        var num2 = random(10);
       var operator = random(2);
       if (operator == 0) {
           var question = prompt(num1 + " + " + num2 + " =");
           var answer = num1 + num2;
       }
       else {
           var question = prompt(num1 + " - " + num2 = " =");
           var answer = num1 - num2;
       }
       if (question == answer) {
           alert("Correct!");
           score++;
       }
       else {
           alert("Incorrect ):");
       }
    }
    document.write(score);
}

这是怎么回事?

2 个答案:

答案 0 :(得分:1)

在您的行var question = prompt(num1 + " - " + num2 = " =");中,您拼写了一个错误的“ +”,即代码中的“ =”。

应该是:

var question = prompt(num1 + " - " + num2 + " =");

与@Nick Parsons在评论中所说的相同

答案 1 :(得分:1)

应通过更改

修复语法错误
var question = prompt(num1 + " - " + num2 = " =");

var question = prompt(num1 + " - " + num2 + " ="); 

我相信这是意图