特定の文字列配列オブジェクトへの変数の比較、これは私のコードです:私はJavaScriptに新たなんだと、単純な推測色のゲームをやろうとしてJavaScriptで
<!doctype html>
<html>
<head>
<title>Guessing Color Game</title>
</head>
<body onload="do_game()">
<script>
var target;
var colors;
var finished = false;
var guess_input;
var guesses = 0;
var random_number = Math.random() * 11;
var random_number_index = Math.floor(random_number);
colors = ["Aqua", "Black", "Blue", "Brown", "Cyan", "Grey", "Green", "Orange", "Red", "White", "Yellow"];
target = colors[random_number_index];
function do_game() {
alert("Hello mate, wanna play a game???");
alert("You gotta guess a color, alright???");
alert("Available colors are: \n\n0. Aqua \n1. Black \n2. Blue \n3. Brown \n4. Cyan \n5. Grey \n6. Green \n7. Orange" +
" \n8. Red \n9. White \n10. Yellow");
alert("Random color to guess is " + target + ", which is index " + random_number_index + " in the array");
while (!finished) {
guess_input = prompt("Write the name of your chosen color please:").toLowerCase();
guesses++;
finished = check_guess();
}
}
function check_guess() {
if (target == guess_input)
return true;
}
</script>
</body>
</html>
私の問題はcheck_guessです()関数:ターゲットがguess_inputと比較されないため、無限ループが発生しています。なぜこれはそうですか?
は、私の答えは役に立ちましたか? –
それは問題のSandeep、説明をありがとう、今は明らかです。 – Fabricio