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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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='container-fluid'>
  <div id='title'>Repeat after me</div>
  <div id='options'><p id='start'>Start</p>Strict? <input type="checkbox" id='strict'><p id='reset'>Reset</p></div>
  <div id='simon'>
    <div class='button dark' id='red'></div>
    <div class='button dark' id='green'></div>
    <div class='button dark' id='blue'></div>
    <div class='button dark' id='yellow'></div>
  </div>
  <div class='hidden' id='wrong'>Wrong :(</div>
  <div class='hidden' id='strictLoss'>Strict mode on, resetting!</div>
  <div class='hidden' id='yay'>You did it!  You won!! Now resetting...</div>
  <div id='count'>Count<p id='totalCount'>0</p></div>
</div>
            
          
!
            
              * {
  height: 100%;
  padding: 0;
  margin: 0;
  font-family: 'Amita', cursive;
}

#title {
  height: auto;
  text-align: center;
  font-size: 2.5em;
}

#start, #strict {
  height: auto;
}

#simon {
  height: 250px;
  width: 250px;
  position: absolute;
  top: 100px;
  left: calc(50% - 125px);
  background-color: #6d6d6d;
  text-align: center;
  border-radius: 5px;
}

.button {
  height: 100px;
  width: 100px;
  border-radius: 10px;
  display: inline-block;
}

#red {
  margin-top: 20px;
  margin-right: 6px;
  background-color: red;
}

#green {
  background-color: #3ff45a;
}

#blue {
  background-color: #4286f4;
  margin-right: 6px;
}

#yellow {
  background-color: yellow;
}

.dark {
  opacity: 0.2;
}

#options {
  position: fixed;
  top: 100px;
  left: 100px;
  font-size: 2em;
}

#count {
  position: fixed;
  top: 100px;
  right: 100px;
  font-size: 2em;
}

#wrong {
  height: auto;
  width: 175px;
  position: fixed;
  top: 150px;
  left: calc(50% - 87.5px);
  z-index: 100;
  color: #ff0000;
  font-size: 3em;
}

#strictLoss {
  height: auto;
  width: 540px;
  position: fixed;
  top: 220px;
  left: calc(50% - 270px);
  z-index: 100;
  color: #ff0000;
  font-size: 3em;
}

#yay {
  height: auto;
  width: 500px;
  position: fixed;
  top: 130px;
  left: calc(50% - 250px);
  z-index: 100;
  color: #00b220;
  font-size: 3em;
  background-color: #303030;
  text-align: center;
  border-radius: 10px;
}

.hidden {
  visibility: hidden;
}
            
          
!
            
              var pattern = [];
var player = [];
var possibilities = ['red', 'green', 'blue', 'yellow'];
var addOne;
var i = 0;
var redAudio = document.createElement('audio');
redAudio.setAttribute('src', 'https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
var blueAudio = document.createElement('audio');
blueAudio.setAttribute('src', 'https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
var greenAudio = document.createElement('audio');
greenAudio.setAttribute('src', 'https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
var yellowAudio = document.createElement('audio');
yellowAudio.setAttribute('src', 'https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
    

$('#start').on('click', function() {
  addCount();
  $('#start').off('click');
})

$('#reset').on('click', function() {
  $('.button').off('click');
  player = [];
  pattern = [];
  $('#totalCount').html(pattern.length);
  $('#start').on('click', function() {
    addCount();
    $('#start').off('click');
  })
})

function addCount() {
  addOne = Math.floor(Math.random() * 4);
  pattern.push(possibilities[addOne]);
  $('#totalCount').html(pattern.length);
  playPattern();
}

function playPattern() {
  setTimeout(function() {
    $('#' + pattern[i]).removeClass('dark');
    if (pattern[i] == 'red'){
      redAudio.play();
    } else if (pattern[i] == 'green'){
      greenAudio.play();
    } else if (pattern[i] == 'blue'){
      blueAudio.play();
    } else if (pattern[i] == 'yellow'){
      yellowAudio.play();
    } 
    setTimeout(button.bind(null, pattern[i]),400);
    i++;
    if (i < pattern.length) {
      playPattern();
    } else if (i == pattern.length) {
      i = 0;
      player = [];
      playerTurn();
    }
  },700);
}

function button(color, delay) {
  $('#' + color).addClass('dark');
}

function playerTurn () {
  $('.button').on('click', function(){
    player.push(this.id);
    if (player.length < pattern.length && 
        player[player.length-1] == pattern[player.length-1]) {
      playButton(this.id);
    } else if (player.length == pattern.length && 
        player[player.length-1] == pattern[player.length-1] &&
        pattern.length < 20) {
      $('.button').off('click');
      playButton(this.id);
      setTimeout(addCount,500);
    } else if (player.length == pattern.length && 
        player[player.length-1] == pattern[player.length-1] &&
        pattern.length == 20) {
      $('#yay').removeClass('hidden');
      $('.button').off('click');
      setTimeout(function(){$('#yay').addClass('hidden');},2000);
      setTimeout(strictLoss,2500);
    } else if (player[player.length-1] != pattern[player.length-1]) {
      $('#wrong').removeClass('hidden');
      $('.button').off('click');
      setTimeout(function(){$('#wrong').addClass('hidden');},1000);
      if (document.getElementById('strict').checked) {
        $('#strictLoss').removeClass('hidden');
        setTimeout(function(){$('#strictLoss').addClass('hidden');},1000);
        setTimeout(strictLoss,1500);
      } else {
        setTimeout(playPattern,1500);
      }
    }
  })
}

function strictLoss() {
  player = [];
  pattern = [];
  $('#totalCount').html(pattern.length);
  addCount();
}

function playButton(box) {
  $('#' + box).removeClass('dark');
  if (box == 'red'){
      redAudio.play();
    } else if (box == 'green'){
      greenAudio.play();
    } else if (box == 'blue'){
      blueAudio.play();
    } else if (box == 'yellow'){
      yellowAudio.play();
    } 
  setTimeout(button.bind(null, box),300);
}
            
          
!
999px
Loading ..................

Console