【发布时间】:2016-08-18 03:10:31
【问题描述】:
我是 javascript 的新手,想知道为什么我的行在这里不起作用,情况如下:
我编写了一个非常简单的猜谜游戏,用户必须从单词列表中猜出随机生成的单词。
我想知道为什么代码没有比较用户的输入来查看是否来自列表?
提前谢谢你:)
var target_index;
var guess_input_text;
var finished=false;
var number_of_guesses = 0;
var colors = ["pink", 'olive', 'lime', 'orange', 'yellow', 'purple', 'indigo', 'blue', 'gray', 'black'];
var guesses = 0;
var color;
function do_game() {
var random_number = Math.random()*9;
var random_number_intiger = Math.floor(random_number);
target_index = random_number_intiger;
color = colors[target_index];
alert(color);
while (!finished){
guess_input_text = prompt('I am thinking of one of these colors.\n\n' + colors + '\n\n What do you think it is?');
guesses++;
alert(guess_input_text);
finished = check_guess();
}
}
function check_guess(){
if (guess_input_text===color){ //this works
return true;
}
if (guess_input_text!=color && guess_input_text!=colors){ //why this is not working :(
alert('Please choose your color among the colors listed below:\n\n' + colors);
return false;
}
if (guess_input_text!=color && guess_input_text==colors){ //this is not working too
alert('You are a bit off');
return false;
}
}
【问题讨论】:
-
您不能将字符串
guess_input_text与数组colors进行比较。它永远是不平等的。
标签: javascript arrays string if-statement compare