未捕获的ReferenceError:赋值While循环中无效的左手边错误
Uncaught ReferenceError: Invalid left-hand side in assignment While loop error
当我尝试运行此代码时,我在控制台中得到一个错误,但我不知道为什么!它是这样说的:未捕获的ReferenceError:赋值的左边无效,它指向第31行
也就是这一行:if (colors.indexOf(String(guess_input))=-1){
基本上我要做的是检查输入的字符串是否是我的预定义数组的一部分,你可以在下面的代码中看到。游戏的终点是一个猜谜游戏,用户必须猜出一种颜色。
<!DOCTYPE >
<html>
<head>
<title>Welcome To The JS Guessing Game</title>
</head>
<body onload="do_game()">
<script>
//Initialize the variables that will be needed.
var target;
var target_index;
var guess_input;
var guesses=0;
var finished=false;
var guesses=0;
var colors = ["blue", "cyan", "gold", "green", "magenta", "orange", "red", "white", "yellow"];
function do_game(){
var random_number = (Math.random()*colors.length);
var random_number=Math.floor(random_number);
target=colors[random_number];
alert(target);
while (!finished){
guess_input=prompt("I am thinking of a color in the list below. Can you guess which color?"+"'n'n"+ colors.join() );
guesses++;
finished = check_guess(); //so finished in the function has to return true for this condition to be met.
}
}
function check_guess(){
if (colors.indexOf(String(guess_input))=-1){
alert("I'm sorry, I do not recognize that color! Try again.");
return true;
}
else if (guess_input > target){
alert("Your guess is alphabetically higher than the correct answer.");
return true;
}
else if (guess_input < target){
alert("Your guess is alphabetically lower than the correct answer. ");
return true;
}
else {
myBody=document.getElementsByTagName("body")[0];
myBody.style.background=target;
alert("You are right! You took "+guesses+" guesses!");
return false;
}
}
</script>
</body>
</html>
结果是第一个警告窗口显示告诉我颜色,然后提示,但在那之后,不管输入,事情就停止了:(如果你能帮忙,我将不胜感激。
欢呼,大卫
变化
if (colors.indexOf(String(guess_input))=-1){
f (colors.indexOf(String(guess_input))==-1){
你的脚本基本上试图将值-1赋值给不是变量的东西
相关文章:
- jQuery:循环一个具有不同超时值的循环
- 在循环中分配json值时,值被覆盖
- 如何在下面的ES6循环中获得前面的文本
- 为什么“;未定义的“;在JavaScript中结束循环
- Javascript循环不会自我更新
- 如何使用jquery处理php循环通过元素
- 而循环只设置php中输入字段中的第一个值
- 循环遍历数组中的特定索引
- Javascript返回值只在循环中返回一次
- 按照选项卡索引的顺序循环一个jQuery选择
- 循环遍历以数组为值的Javascript对象
- 为什么JavaScript在for循环为3时向所有4发出警报
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 循环结束/推送到数组之前在页面上呈现EJS
- 如果我在javascript中输入无效的电子邮件或空白,如何显示特定的文本框边框红色
- 未捕获的不变冲突:元素类型无效(react、webpack、循环导入)
- 循环不应在遇到无效语句时中断
- 动画永远循环在Chrome中有效,但在IE中无效
- JavaScript中的循环通过复选框无效
- 未捕获的ReferenceError:赋值While循环中无效的左手边错误