【发布时间】:2016-02-08 07:13:14
【问题描述】:
/*Below is a color guessing game that is supposed to run in browser but when I open the code below in my Chrome, nothing happens. Any ideas? I would appreciate any help.*/
/*Below is a color guessing game that is supposed to run in browser but when I open the code below in my Chrome, nothing happens. Any ideas? I would appreciate any help.*/
var colors = ["Aqua", "Black", "Blue", "Brown", "Coral", "Crimson", "Cyan","Fuchsia", "Gold", "Gray", "Green"];
var target;
var guess_input_text;
var guess_input;
var finished = false;
var guesses = 0;
function do_game() {
var random_number = Math.random() * colors.length - 1;
var target_index = Math.floor(random_number);
target = String(colors[target_index]);
//what is wrong below? Why is this not working in my Chrome browser.
while (!finished) {
guess_input_text = prompt("I am thinking of these colors:\n\n"
"Aqua, Black, Blue, Brown, Coral, Crimson, Cyan, Fuchsia, Gold, Gray, Green\n\n"
"What color am I thinking of?");
guess_input = String(guess_input_text);
guesses += 1;
finished = check_guess();
}
}
/* Is my first if statement correct? Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?*/
function check_guess(){
if (guess_input != "Aqua"||"Black"||"Blue"||"Brown"||"Coral"||"Crimson"||"Cyan"||"Fuchsia"||"Gold"||"Gray", "Green") {
alert("Sorry, I don’t recognize your color.\n\nPlease try again.");
}
if(guess_input < target){
alert("Sorry, your guess is not correct!\n\nHint: your color is alphabetically higher than mine.");
return false;
}
if(guess_input > target){
alert("Sorry, your guess is not correct!\n\nHint: your color is alphabetically higher than mine.");
return false;
}
alert("Congratulations! You have guessed the color!\n\n"
"It took you " + guesses + " to finish the game!\n\n"
"You can see the color in the background.");
return true;
}
【问题讨论】:
-
请不要(试图)在 cmets 中描述您的问题。在代码之外写一个适当的描述。一遍又一遍地重复同样的句子也没什么用。
-
有什么问题?
-
if(guess_input != "Aqua"||"Black"||"Blue"||"Brown"||"Coral"||"Crimson"||"Cyan"||"Fuchsia"||"Gold"||"Gray", "Green")将始终评估为true因为整个事情将评估为"Green"这是真实的。换句话说,您的if语句在语义上是不正确的。 -
您的代码有几个部分在语义或句法上不正确。请查看控制台是否有错误并使用 JSHint。
标签: javascript arrays loops while-loop