2016-03-26 15 views
0

Bu kodun neden çalışmadığı konusunda tamamen clueless. Bir renk tahmin oyunu olması gerekiyordu. Bir kez koştu ve sonra sadece boş bir ekran gösteriyor. Bir dizi veya renkten rasgele bir renk seçilmesi ve daha sonra kullanıcı girişini sorması ve seçilen renk ve çıktıları buna göre karşılaştırması gerekir.Javascript - Hatanın nerede olduğunu bulamıyorum.

<!doctype html> 
 
<html> 
 
<head> 
 
    <title>JavaScript Guessing Game</title> 
 
    <script> 
 
     var target;    
 
     var guess_input_text; 
 
     var guess_input;  
 
     var finished = false; 
 
     var guesses = 0; 
 
     var colors = ["blue","cyan","gold","grey","green","magenta", "orange", "red","white","yellow"]; 
 
     var scolors = colors.sort(); 
 

 
     function do_game() { 
 
      var random_number = Math.random() * 10; 
 
      var random_number_integer = Math.floor(random_number); 
 
      target_index = random_number_integer; 
 
      target = colors[target_index]; 
 

 
      while (!finished) { 
 
       guess_input = (prompt("I am thinking of one of these colors " + colors.join(", ") + ".\n\n What color am I thinking of?")).toLowerCase(); 
 
       guesses += 1; 
 
       finished = check_guess(); 
 
      } 
 
     } 
 

 
     function check_guess() { 
 
      if (!(colors.indexOf(guess_input) >= 0)) { 
 
       alert("Sorry I don't recognize your color\n\n" + 
 
         "Please try again."); 
 
       return false; 
 
      } 
 
      if (scolors.indexOf(guess_input) > (scolors.indexOf(target))) { 
 
       alert("Sorry, your guess is not correct\n\n" + "Hint: Your color is alphabetically higher than mine\n\n"+"Please try again."); 
 
       return false; 
 
      } 
 
      if (scolors.indexOf(guess_input) < (scolors.indexOf(target))) { 
 
       alert("Sorry, your guess is not correct\n\n" + "Hint: Your color is alphabetically lower than mine\n\n"+"Please try again."); 
 
       return false; 
 
      } 
 
      else{ 
 
      alert("Congratulations! You've guessed the color!+ 
 
        ".\n\nIt took you " + guesses + 
 
        " to finish the game!\n\n You can see the color in the background"); 
 
      return true; 
 
     } 
 
     } 
 
    </script> 
 
    
 
</head> 
 
<body onload="do_game()"> 
 

 
</body> 
 
</html>

+1

Sayfayı tarayıcınıza yükleyin. F12 tuşuna basın ve ardından konsol sekmesini seçin. Kodunuzu yeniden çalıştırmak için F5 tuşuna basın. Herhangi bir hata varsa, konsol sekmesinde görünecektir. – jeff

cevap

3

Sen eksik çift tırnak:

alert("Congratulations! You've guessed the color!"+ 
//-----------------------------------------------^ 

Ben, (krom ctrl + shift + i olarak) tarayıcınızda konsolu açmanızı tavsiye kodunuzu çalıştırmak ve ilk hatalara olmazdı bir dahaki sefer.

+0

Teşekkürler @ madox2, kesinlikle tavsiyelerinizi kullanacaktır. – ankitom

+0

@ankitom Bir şey değil :-) – madox2

-1

Eğer bir JavaScript IDE kullanıyorsanız, ör. atom, webstorm ve yüce. hataları kolayca bulabilirsiniz.

enter image description here

İlgili konular