2016-08-20 3 views
-1

Arbeite durch einen Intro JS Kurs Kurs. Ich mache ein einfaches Ratespiel. Ich habe am Anfang ein paar Warnungen zur Fehlerbehebung eingefügt, aber ich kann dieses Programm nicht erreichen, um die Warnung zu erhalten ("test1"); Linie in meinem PlayGame, welches sofort durch das Body-Element geladen wird. Irgendwelche Ideen, was ich falsch mache? Ich scheine es einfach gut zu erklären ...Fehler beim Deklarieren von Array-JavaScript?

<!DOCTYPE html> 
<html> 
<body onload="playGame()"> 
<p>Welcome to my color guessing game</p> 
<script language = "JavaScript"> 
function inArray(needle, haystack){ 
    for (var i = 0; i <haystack.length(); i++){ 
     if (needle === haystack[i]){ 
      return true; 
     } 
    } 
    return false; 
} 
function changeBackground(color){ 
    document.body.style.background = color; 
} 
function playGame(){ 
    var correct = false; 
    alert("Correct status is: " + correct); 
    var colorArray = ["cyan", "gold", "green", "gray", "magenta", "blue", "red", "orange", "yellow", "white"]; 
    alert("test1"); 
    for (var i = 0; i < colorArray.length(); i++){ 
     alert("for " + i + " the color is " + colorArray[i]); 
    } 
    colorArray = colorArray.sort(); 
    alert("answer index is " + answerIndex); 
    alert("color array length is " + colorArray.length()); 
    var answerIndex = Math.floor(Math.random()*colorArray.length()); 
    alert("the resulting color from the color array is: " + answerColor); 
    var answerColor = colorArray[answerIndex]; 
    alert("The correct color is " + answerColor); 
    var answerList = colorArray.join(", "); 
    var guessCount = 0; 
    while(!correct){ 
     var colorGuess = prompt("Welcome to my guessing game! The colors available for your choosing are: " + "\n\n" + answerList + "\n\n" + "Which color am I thinking of?"); 
     guessCount++; 
     if (!inArray(colorGuess, colorArray)){ 
      alert("Your guess wasn't one of the selections that was available or I otherwise don't recognize it." + "\n\n" + "Please try again!"); 
     } 
     else{ 
      if (colorArray.indexOf(colorGuess)<color.indexOf(answerColor)){ 
       alert("Your guess was alphabetically before the correct color! Try again."); 

      } 
      else if (colorARray.indexOf(colorguess)>color.indexOf(answerColor)){ 
       alert("Your guess was alphabetically after the correct color! Try again."); 
      } 
      else{ 
       alert("Your guess is correct!"); 
       correct = true; 
       changeBackground(answerColor); 
      } 
     } 
    } 
    alert("Great job!" + "\n\n" + "You took " + guessCount + " guesses to get the correct answer!"); 
} 
</script> 
</body> 
</html> 
+0

auch überprüfen Sie bitte, ob Sie einen Fehler in der Konsole – Ripun

+0

.length bekommen() => .length –

+0

* "... kann nicht ... den' alert ("test1") erreichen; 'line" * Sagen Sie, dass * der Alarm nur vor der 'var colorArray = ...' Zeile angezeigt wird, nicht aber der einer nach? – nnnnnn

Antwort

-1

Ich habe gerade Ihren Code getestet.

alert("answer index is " + answerIndex); 
      alert("color array length is " + colorArray.length()); 
      var answerIndex = Math.floor(Math.random()*colorArray.length()); 
      alert("the resulting color from the color array is: " + answerColor); 
      var answerColor = colorArray[answerIndex]; 
      alert("The correct color is " + answerColor); 

Hier Sie versuchen, die Variable answerIndex, aufmerksam zu machen, bevor Sie es erklären.

+2

Das ist mehr wie ein Kommentar für mich. –

+0

Dies würde nur "undefined" alarmieren, es würde die Funktion nicht anhalten. – nnnnnn

+0

Nur teilen, was ich falsch im Code gefunden habe. –

0

In JS length ist eine Eigenschaft, die die Anzahl der Elemente in einem Array zurückgibt. MDN Array Length.

In colorArray.length() ersetzen length() von length.

+0

Würde mir bitte jemand sagen, was in meiner Antwort falsch ist und warum wird es abgelehnt? – Nikhil

-1

Es gibt viele dumme JavaScript-Syntax Fehler, Fest alle und unten ist die Laufcode,

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8" /> 
    </head> 
    <body onload="playGame()"> 
     <p>Welcome to my color guessing game</p> 
     <script type="text/javascript"> 
      function inArray(needle, haystack){ 
       for (var i = 0; i <haystack.length; i++){ 
        if (needle === haystack[i]){ 
         return true; 
        } 
       } 
       return false; 
      } 
      function changeBackground(color){ 
       document.body.style.background = color; 
      } 
      function playGame(){ 
       var correct = false; 
       alert("Correct status is: " + correct); 
       var colorArray = ["cyan", "gold", "green", "gray",  
        "magenta", "blue", "red", "orange", "yellow",  
        "white"]; 
       alert("test1" + colorArray.length); 
       for (var i = 0; i < colorArray.length; i++){ 
        alert("for " + i + " the color is " + 
         colorArray[i]); 
       } 
       colorArray = colorArray.sort(); 
       alert("answer index is " + answerIndex); 
       alert("color array length is " + 
        colorArray.length); 
       var answerIndex = 
        Math.floor(Math.random()*colorArray.length); 
       alert("the resulting color from the color array 
        is: " + answerColor); 
       var answerColor = colorArray[answerIndex]; 
       alert("The correct color is " + answerColor); 
       var answerList = colorArray.join(", "); 
       var guessCount = 0; 
       while(!correct){ 
        var colorGuess = prompt("Welcome to my guessing 
         game! The colors available for your choosing 
         are: " + "\n\n" + answerList + "\n\n" + "Which 
         color am I thinking of?"); 
        guessCount++; 
        if (!inArray(colorGuess, colorArray)){ 
         alert("Your guess wasn't one of the selections 
         that was available or I otherwise don't 
         recognize it." + "\n\n" + "Please try 
         again!"); 
        } 
        else{ 
         if (colorArray.indexOf(colorGuess) 
          <colorArray.indexOf(answerColor)){ 
          alert("Your guess was alphabetically 
           before the correct color! Try 
           again."); 
        } 
        else if(colorArray.indexOf(colorGuess) > 
         colorArray.indexOf(answerColor)){ 
         alert("Your guess was alphabetically after the 
          correct color! Try again."); 
        } 
        else{ 
         alert("Your guess is correct!"); 
         correct = true; 
         changeBackground(answerColor); 
        } 
       }  
      } 
      alert("Great job!" + "\n\n" + "You took " + guessCount + " 
      guesses to get the correct answer!"); 
     } 
    </script> 
    </body> 
</html> 
+0

Danke Paul! :) Das war hilfreich für mein Lernen. – ssilver

+0

Wenn Sie meine Antwort hilfreich gefunden haben, bitte markieren Sie sie als richtige Antwort - @ ssilver –

+0

Würde mir bitte jemand sagen, was in meiner Antwort falsch ist und warum ist es downvoted? –

Verwandte Themen