2017-07-22 4 views
1

Ich habe einen einfachen JavaScript-Code, aber es funktioniert nicht. Mein Code ist:html Körper onload funktioniert nicht

<html> 
<head> 
    <title>JavaScript</title> 
<script>  
var target; 
var guess_input; 
var guesses = 0; 
var finished = false; 
var colors = ["black","blue","green","purple","red","white","yellow"]; 



     function do(){ 


      var random = Math.random(); 
      var index = Math.floor(random * 7); 
      target = colors[index]; 
      while(!finished){ 
       guess_input = prompt("I am thinking of a color : black, blue, green, purple, red, white \n\n" + "What color am i thinking of?"); 
       guesses++; 
       finished = check();    
      } 
      var myBody = document.getElementsByTagName("body")[0]; 
      myBody.style.background = target; 
     } 

     function check(){ 
      if (guess_input > target){ 
       alert("your color is alphabetically higher than mine"); 
       return false; 
      } 

      if (guess_input < target){ 
       alert("your color is alphabetically lower than mine"); 
       return false; 
      } 

      if (guess_input == target){ 
       alert("your color is correct! it tooks you" + guesses "guesses to finish the game!"); 
       return true; 
      } 
      else { 
       alert("Sorry, I do not recoginize your color"); 
       return false; 
      } 
</script> 
</head> 
    <body onload = "do()"> 

    </body> 

</html> 

Die Fehlermeldung lautet: Uncaught Syntaxerror: unerwartetes Token tun part2.html: 53 Uncaught Syntaxerror: unerwartetes Token), die "Funktion tun()" ist, so was ist das Problem? Vielen Dank!

+0

benennen Sie die Funktion haben, und versuchen Sie es erneut. Dies könnte ein Keyword-Problem sein. –

+3

Es ist in der Tat ein Keyword-Problem. In Javascript gibt es etwas, das 'do {} while()' heißt, und 'do' als Funktionsname steht in Konflikt damit, und' do' ist ein ** reserviertes Schlüsselwort **, das nicht als Name für Funktionen verwendet werden kann , Variablen usw. – adeneo

Antwort

0

Sie zum Beispiel auf andere schließen Klammern zur Funktionskontrolle und ändern Sie Funktionsname fehlt doSomething

<html> 
    <head> 
     <title>JavaScript</title> 
    <script>  
    var target; 
    var guess_input; 
    var guesses = 0; 
    var finished = false; 
    var colors = ["black","blue","green","purple","red","white","yellow"]; 
      function doSomething(){ 


       var random = Math.random(); 
       var index = Math.floor(random * 7); 
       target = colors[index]; 
       while(!finished){ 
        guess_input = prompt("I am thinking of a color : black, blue, green, purple, red, white \n\n" + "What color am i thinking of?"); 
        guesses++; 
        finished = check();  
       } 
       var myBody = document.getElementsByTagName("body")[0]; 
       myBody.style.background = target; 
      } 

      function check(){ 
       if (guess_input > target){ 
        alert("your color is alphabetically higher than mine"); 
        return false; 
       } 

       if (guess_input < target){ 
        alert("your color is alphabetically lower than mine"); 
        return false; 
       } 

       if (guess_input == target){ 
        alert("your color is correct! it tooks you" + guesses "guesses to finish the game!"); 
        return true; 
       } 
       else { 
        alert("Sorry, I do not recoginize your color"); 
        return false; 
       } 
      } 
    </script> 
    </head> 
    <body onload="doSomething()"> 

    </body> 

    </html> 
Verwandte Themen