2016-08-20 33 views
-1

通过介绍JS coursera课程。我正在做一个简单的颜色猜谜游戏。我在开始时插入了一些警报来进行故障排除,但我无法使此程序获得警报(“test1”);在我的playGame中,通过body元素立即加载。任何想法我做错了什么?我似乎宣称它很好...声明数组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

也请检查您是否收到任何错误控制台 – Ripun

+0

。长度()=>。长度 –

+0

* “......不能......达到alert(”test1“);'line”*你是说它*在* var colorArray = ...行之前显示警报,而不是一个后? – nnnnnn

回答

-1

我刚刚测试过你的代码。

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); 

在这里,我们试图提醒变量answerIndex,你声明之前。

+2

这更像是对我的评论。 –

+0

这只会提醒'未定义的',它不会停止继续的功能。 – nnnnnn

+0

只是分享我在代码中发现错误的地方。 –

0

在JS length是一个属性,它返回数组中的元素数量。 MDN Array Length

colorArray.length()替换length()length

+0

请问有人会告诉我我的答案中出现了什么问题,为什么它会被低估? – Nikhil

-1

有很多愚蠢的JavaScript语法错误,所有的固定和波纹管运行代码,

<!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

谢谢保罗! :)这对我的学习很有帮助。 – ssilver

+0

如果您发现我的答案有帮助,请将其标记为正确答案 - @ ssilver –

+0

请问有人会告诉我我的回答中出现了什么问题,为什么问题得到解决? –