我是JS的新手,正在制作一个小型的数学游戏。在JSHint上它说我在一行上缺少一个分号,我不是,它不会在我的浏览器中运行,我会发布代码,有人可以告诉我我做错了什么。谢谢!
var newMathGame = function() {
var points = 1;
var numberOfQuestions = prompt("How many questions would you like?");
var highestNumber = prompt(
"What is the highest number you would like to be quizzed on?");
var whichGame = prompt(
"Would you you like to play\n 'SUBTRACTION', 'ADDITION', 'MULTIPLICATION' or 'DIVISION'?"
).toUpperCase();
switch (whichGame) {
case 'ADDITION':
addition();
break;
case 'SUBTRACTION':
subtraction();
break;
case 'DIVISION':
division();
break;
case 'MULTIPLICATION':
multiplication();
break;
}
function subtraction() {
for (var count = 0; count < numberOfQuestions; count++) {
var x = Math.floor((Math.random() * 10) + 1);
var y = Math.floor((Math.random() * 10) + 1);
if (x < y) {
subtraction();
} else {
var answer = (x - y);
var user_answer = prompt(x + "-" + y);
if (answer == user_answer) {
alert("Yes!");
} else {
alert("No! The answer is " + answer);
}
}
}
}
function addition() {
for (var count = 0; count < numberOfQuestions; count++) {
var x = Math.floor((Math.random() * 10) + 1);
var y = Math.floor((Math.random() * 10) + 1);
var answer = (x + y);
var user answer = (x + "+" + y); //says I am missing semicolon on this line
if (answer == user_answer) {
alert("Yes!");
} else {
alert("No! The answer is " + answer);
}
}
}
};
答案 0 :(得分:4)
变量名user answer
无效,因为它有空格。看起来应该是user_answer
。
答案 1 :(得分:1)
您的问题是该变量之前被称为user_answer
,但您的行说明要检查的变量称为user answer
。 JavaScript和许多其他编译器使用单字变量。要让编译器停止翻转,请在错误所在的位置添加下划线,使其显示为user_answer
。
答案 2 :(得分:1)
这一行在语法上是不正确的。变量名称不能包含空格。
var user answer = (x + "+" + y);
调试器可能认为你想要
var user;
answer = (x + "+" + y);
这将非常有意义。