cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="content">
  
  <div class="row top">
    <div class="box">
      <div class="dialogue" id="dialogue">SIMON</div>
    </div>
    <div class="box">
      <div class="countertext" id="countertext">COUNT:</div>
      <div id="counter" class="counter">0</div>
      
    </div>
  
  </div>
   
  <div class="row">
    <button id="red" class="simon" value="0"></button>
    <button id="yellow" class="simon" value="1"></button>
    <button id="green" class="simon" value="2"></button>
    <button id="blue" class="simon" value="3"></button>
  </div>
  
  <div class="row">
    
    <div class="box">
      <button id="start" class="controls"></button>
      <div class="buttontext" id="starttext">START</div>
    </div>
  
    <div class="box">
      <button id="strict" class="controls"></button>
      <div class="buttontext" id="stricttext">STRICT</div>
    </div>
    
  </div>

</div>

 <audio id="redAudio" class="audio" src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"></audio>

 <audio id="yellowAudio" class="audio" src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"></audio>

 <audio id="greenAudio" class="audio" src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"></audio>

 <audio id="blueAudio" class="audio" src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"></audio>


 <audio id="winAudio" class="audio" src="http://www.freesound.org/data/previews/337/337049_3232293-lq.mp3"></audio>

 <audio id="loseAudio" class="audio" src="https://www.freesound.org/data/previews/327/327738_3694529-lq.mp3"></audio>
            
          
!
            
              .content {
  
  text-align: center;
  padding: 30px;
  width: 700px;
  margin: 0 auto;
  font-family: sans-serif;
  font-weight: bold;
  color: #29335C;
  letter-spacing: 2px;
}

button {  
  border-radius: 5px;
  border: none;
  outline:none; 
}
button:hover {
  opacity: 0.5;
}

.controls {
  
  width: 20px;
  height: 20px;
  margin: 10px;
  background-color: lightgrey;
}

.simon {
  
  width: 120px;
  height:120px;
  margin: 20px;
}

.simon:hover {
  
    opacity: 0.5;
}

.box {
  display:inline-block;
  padding: 10px;
}

#red {
  background-color: #E4572E;
}

#yellow {
  background-color: #F3A712;
}

#green {
  background-color: #A8C686;
}

#blue {
  background-color: #669BBC;
}

.dialogue {
  text-align: left;
  width: 200px;
  padding: 5px;
  
}

.counter, .countertext {
  display: inline;
}

.top {
  background-color: whitesmoke;
  margin: 5px;
  border-radius: 5px;
}
            
          
!
            
              $(document).ready(function() {
  var simon = $(".simon"); // Array of simon buttons. red is simon[0], yellow is simon[1], green is simon[2], blue is simon[3].
  var audio = $(".audio");
  var startButton = $("#start");
  var strictButton = $("#strict");
  var startText = $("#starttext");
  var strictText = $("#stricttext");
  var count = 0;
  var clicks = 0;
  var userTurn = false;
  var wait = 800;
  var array = [];
  var userArray = [];
  var strict = false;
  var counter = $("#counter");
  var goal = 20;

  function createArray() {
    var arr = [];
    for (var i = 0; i < 20; i++) {
      arr.push(Math.floor(Math.random() * 4));
    }
    return arr;
  }

  //play function -  plays sound of selected button. Button is a number referring to a simon button.
  function playButton(buttonId) {
    // e.g. playButton(1) {
    audio[buttonId].play();
   
    $(simon[buttonId]).animate({opacity: "toggle"}, wait/5);
    $(simon[buttonId]).animate({opacity: "toggle"}, wait/4);
  }

  // stops the game, clears array etc.

  function stopGame() {
    if (count == goal) {
      $("#dialogue").html("VICTORY!");
      audio[4].play();
    } else {
      $("#dialogue").html("GAME OVER");
    }

    userTurn = false;
    count = 0;
    counter.html(count);
    clicks = 0;
    startButton.css("background-color", "");
    startText.html("START");
    array = [];
    userArray = [];
  }

  // alerts user to mistake if input is incorrect, and either ends game (if in strict mode) or replays last sequence
  function mistake() {
    userTurn = false;
    $(simon).animate({opacity: "toggle"}, wait/5);
    $(simon).animate({opacity: "toggle"}, wait/4);
    audio[5].play();

    if (!strict) {
      setTimeout(function() {
        playSounds();
      }, wait);
    } else {
      stopGame();
    }
  }

  // plays array of sounds up to current count
  function playSounds() {
    userTurn = false;
    var playBack = 0;
    var intervalId = setInterval(function() {
      if (playBack <= count) {
        playButton(array[playBack]);
        playBack++;
      } else {
        clearInterval(intervalId);
        playBack = 0;
        userTurn = true;
        return;
      }
    }, wait);
  }

  function runGame(clicked) {
    clicks++;
    playButton(clicked);
    userArray.push(clicked);

    // compare user's click to current count in simon array. if match continue, else endgame.

    if (userArray.toString() !== array.slice(0, clicks).toString()) {
      mistake();
      userArray = [];
      clicks = 0;
    } else {
      if (clicks == count + 1) {
        userTurn = false;
        count++;

        if (count == goal) {
          stopGame();
          return;
        }

        counter.html(count);
        counter.innerHTML = 0;
        playSounds();
        clicks = 0;
        userArray = [];
      }
    }
  }

  strictButton.click(function() {
    if (strict === false) {
      strict = true;
      strictButton.css("background-color", "darkred");
    } else {
      strict = false;
      strictButton.css("background-color", "");
    }
  });

  startButton.click(function() {
    if (userTurn === false) {
      $("#dialogue").html("SIMON");
      userTurn = true;
      startButton.css("background-color", "darkred");
      startText.html("STOP");

      array = createArray();
      playSounds();
    } else {
      stopGame();
    }
  });

  //listens for simon clicks and calls playButton when clicked
  simon.click(function() {
    if (userTurn) {
      runGame($(this).attr("value"));
    }
  });
});

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console