简体   繁体   English

我正在使用 javascript 创建一个 simon 游戏,但我在控制台上遇到错误:

[英]I was creating an simon game using javascript but i got an error on console:

Please give me the solution for this code I was creating an simon game using javascript but i got an error on console:请给我这个代码的解决方案我正在使用 javascript 创建一个西蒙游戏,但我在控制台上遇到错误:

The error: Uncaught ReferenceError: randomChosenColour is not defined at game.js:3:20 (anonymous) @ game.js:3错误:Uncaught ReferenceError: randomChosenColour is not defined at game.js:3:20 (anonymous) @game.js:3

index.html file code: index.html文件代码:

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Simon</title>
  <link rel="stylesheet" href="styles.css">
  <link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
</head>

<body>
  <h1 id="level-title">Press A Key to Start</h1>
  <div class="container">
    <div lass="row">

      <div type="button" id="green" class="btn green">

      </div>

      <div type="button" id="red" class="btn red">

      </div>
    </div>

    <div class="row">

      <div type="button" id="yellow" class="btn yellow">

      </div>
      <div type="button" id="blue" class="btn blue">

      </div>

    </div>

  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script src="game.js"></script>
</body>

</html>

game.js code:游戏.js代码:

var buttonColours = ["red", "blue", "green", "yellow"];
var gamePattern = [randomChosenColour];

function nextSequence(params) {
    var randomNumber = Math.floor(Math.random() * 4);
    var randomChosenColour = buttonColours[randomNumber];
    gamePattern.push(randomChosenColour);

}

It is because by the time you declare gamePattern , randomChosenColour is not defined yet.这是因为在您声明gamePattern时, randomChosenColour尚未定义

You just need to declare the gamePattern array to be an empty array.您只需将gamePattern数组声明为空数组即可。

var gamePattern = [];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM