css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container">

  <div class="row text-center">
    <button id="begin">Begin round</button>
  </div>

  <div class="simon text-center">
    <div class="row">
      <div class="col-xs-offset-2 col-xs-4">
        <div class="red game-button" id="red" data-tile="1"></div>
      </div>
      <div class="col-xs-4">
        <div class="blue game-button" id="blue" data-tile="2"></div>
      </div>
    </div>
    <div class="row" id="secondRow">
      <div class="col-xs-offset-2 col-xs-4">
        <div class="yellow game-button" id="yellow" data-tile="3"></div>
      </div>
      <div class="col-xs-4">
        <div class="green game-button" id="green" data-tile="4"></div>
      </div>
    </div>
  </div>

  <div class="row">
    <p id="counter" class="text-center">0 Steps</p>
  </div>

  <div class="row text-center">
    <button id="restart">Restart</button>
  </div>

  <div class="row text-center">
    <button id="strict">Not Strict (click to toggle)</button>
  </div>

  <div class="row text-center">
    <p id="wrong"></p>
  </div>
</div>
            
          
!
            
              $bg: #E5A963;
$color: #1F1B0F;

body{
  background: $bg;
  color: $color;
}

button{
  background: $bg;
  border: 2px solid $color;
  border-radius: 20px;
  padding-left: 10px;
  padding-right: 10px;
  text-align: center;
}

#strict{
  margin-top: 4px;
}

.red{
  background: red;
}
.blue{
  background: blue;
}
.yellow{
  background: yellow;
}
.green{
  background: green;
}

.lit{
  background: black;
}

#begin{
  margin-top: 5px;
  margin-bottom: 5px;
}

.stop-clicks {
  pointer-events: none;
}

.simon{
  border: 2px solid black;
  border-radius: 20px;
  width: 50%;
}

.game-button{
  cursor: pointer;
  border-radius: 20px;
  height: 30px;
  width: 100%;
}

button:active{
  outline: none;
  
}

#secondRow{
  margin-top: 5px;
}
            
          
!
            
              var rounds = 0; //number of rounds
var click = 0;
var sequence = []; //order of buttons
var canClick = false;
var strict = false;
var redSound = new
Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
var blueSound = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
var yellowSound = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
var greenSound = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");

//begins round
$("#begin").click(function() {
  if (rounds == 0) {
    newRound(); //begins new round
  }
});

//resets values and restarts game
$("#restart").click(function() {
  rounds = 0;
  $("#counter").html(rounds + " Steps");
  sequence = [];
});

//enables strict mode
$("#strict").click(function() {
  if (rounds == 0 && !strict) {
    strict = true; //begins new round
    $("#strict").html("Strict");
  }
  else if (rounds == 0 && strict) {
    strict = false; //begins new round
    $("#strict").html("Not Strict");
  }
});

//begins round, generates a random number, and calls animate
function newRound() {
  rounds++;
  $("#counter").html(rounds + " Steps");
  sequence.push(Math.floor((Math.random() * 4) + 1)); //push random number between 1 and 4 to sequence
  animate(sequence); //call animate, given current sequence
}

//animates by calling lightUp, then calls clicks
function animate(sequence) {
  $(".game-button").addClass("stop-clicks");
  var i = 0;
  //loop through sequence and run lightUp for each value
  var t = setInterval(function() {
    lightUp(sequence[i]);
    i++;
    if (i >= sequence.length) {
      clearInterval(t);
      $(".game-button").removeClass("stop-clicks");
    }
  }, 1000);
  console.log(sequence);
  canClick = true;
}

//animates sections
function lightUp(tile) {
  if (rounds > 0) {
    //set tile equal to current tile in sequence and give class of lit
    var $tile = $('[data-tile=' + tile + ']').addClass('lit');
    switch(tile){
      case 1: redSound.play();
        break;
      case 2: blueSound.play();
        break;
      case 3: yellowSound.play();
        break;
      case 4: greenSound.play();
        break;
        
    }
    //after 300 milliseconds remove class
    setTimeout(function() {
      $tile.removeClass('lit');
    }, 500);
  }

}

$(".game-button").mousedown(function() {
  var id = $(this).attr("id");
  switch (id) {
    case "red":
      sound = redSound;
      break;
    case "blue":
      sound = blueSound;
      break;
    case "yellow":
      sound = yellowSound;
      break;
    case "green":
      sound = greenSound;
      break;
  };
  sound.play();
  $("#" + id).addClass("lit");
});

$(".game-button").mouseup(function() {
  var id = $(this).attr("id");
  var number;
  $("#" + id).removeClass("lit");

  switch (id) {
    case "red":
      number = 1;
      break;
    case "blue":
      number = 2;
      break;
    case "yellow":
      number = 3;
      break;
    case "green":
      number = 4;
      break;
  };
  //checks if click number equals sequence length
  //if wrong
  if (sequence[click] != number) {
    click = 0;
    alert("Wrong!");
    if(!strict){
      animate(sequence);
    }
    if(strict){
      rounds = 0;
      $("#counter").html(rounds + " Steps");
      sequence = [];
    }
  } 
  
  //if correct
  else if (sequence[click] == number) {
    click++;
    //victory
    if(click == 20){
      click = 0;
      alert("Victory screech! LOOLOOLOO");
      rounds = 0;
      $("#counter").html(rounds + " Steps");
      sequence = [];
    }
    //if last in sequence
    else if (click == rounds) {
    click = 0;
    newRound();
  } 
    
  }
  
});
            
          
!
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