2016-09-26 5 views
0
<html> 
    <head> 
    <script> 
var play; 
var target; 
var count = 0; 
var guess; 
var colors = ["blue ", "coral ", "orchid ", "red ", "cyan ", "dark orange", "aqua ", "yellow ", "gold ", "brown "]; 

var colors = colors.sort(); 

function do_this() { 
    var choose = Math.floor(Math.random() * (colors.length)); 
    target = colors[choose]; 
    alert("The correct answer is " + target); 
    alert(typeof(colors)); 
    while (true) { 
    play = prompt("I am thinking one of these colors\n\n" + colors + "What color am I thinking of?"); 
    guess = colors.indexOf(play); 

    count = count + 1; 
    if (guess == -1) { 
     alert("Sorry, I don't recognize your color"); 
     continue; 
    } 
    if (guess > target) { 
     alert("Sorry, your guess is not correct\n\n" + "Hint: your color is alphabetically higher than mine\n\n Please try again"); 
     continue; 
    } 
    if (guess < target) { 
     alert("Sorry, your guess is not correct\n\n" + "Hint: your color is alphabetically lower than mine\n\n Please try again"); 
     continue; 
    } 
    alert("Congratulations! You have guessed the color\n\n!" + "It took you" + count + "guesses to finish the game\n\n" + 
     "You can see the color of the background"); 
    return true; 
    } 
} 

    </script> 
    <body onload = "do_this()"> 
    </body> 
    </head> 
    </html> 

正しい色を入力しても実行しようとすると、「あなたの色を認識できません」と表示されますが、-1と表示されていると思われる値を確認しようとしました。助けてください!Javascript:このコードで何が問題になっていますか?

+0

プロンプトで色名の後にスペースを入力すると機能します –

答えて

1

var colors = ["blue ", "coral ", "orchid ", "red ", "cyan ", "dark orange", "aqua ","yellow ", "gold ", "brown "];4

各ワードは、それの後にスペースがあります。スペースを取り除くと機能します。また、4とは何ですか?あなたもそれを削除することができます。

0

各色の名前の後ろに二重引用符の後にスペースがあります。あなたがスペースを削除する場合は、行くべきである。

関連する問題