我在javascript全新的字符串值,不知道爲什麼我沒有線在這裏工作,這裏的情況是:的javascript:if語句比較不工作
我寫了一個非常簡單的猜謎遊戲,其中用戶必須從單詞列表中猜測隨機生成的單詞。
我不知道爲什麼代碼是不是比較用戶的輸入,看是否是從列表或沒有?
預先感謝您:)
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;
}
}
你不能用一個數組'colors'比較字符串'guess_input_text'。它永遠是不平等的。 – 4castle