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.

            
              <body>
  <div id="simon">
    <div id="top">
      <div id="green" class=""></div>
      
      <div id="red" class=""></div>
    </div>
    <div id="bottom">
      <div id="yellow" class=""></div>
      <div id="blue" class=""></div>
    </div>
    <div id="middle">
      <h1>Simon</h1>
      <div id="items-div">
        <div id="count-div">
          <p id="count"  class="off">--</p>
          <p>Count</p>
        </div>
        <div id="start-div">
          <button id="start"></button>
          <p>Start</p>
        </div>
        <div id="strict-div">
          <button id="strict"></button>
          <p id="strict-indicator" class="off"></p>
          <p>Strict</p>
        </div>
      </div>
      <div id="switch">
        <div id="slider" class="off"></div>
      </div>
    </div>
  </div>
</body>
            
          
!
            
              body {
  font-size: 62.5%;
  font-family: 'Alfa Slab One';
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  background-image: url(https://res.cloudinary.com/santiago-g-mez/image/upload/v1467996274/bg_texture___wood_by_nortago_ap41ab.jpg);
  text-align: center;
}

#simon {
  background-color: gray;
  width: 500px;
  height: 500px;
  border: solid black 20px;
  border-radius: 50%;
  -moz-border-radius: 50%;
  -webkit-border-radius: 50%;
  overflow: hidden;
  margin: 3% auto;
  box-shadow: 0px 0px 5px black;
}

#top, #bottom {
  display: block;
  width: 500px;
}

#top div, #bottom div {
  display: inline-block;
  width: 230px;
  height: 230px;
  float: left;
  margin: 0;
}

#green {
  background-color: #0cc90b;
  border-right:10px solid black;
  border-bottom:10px solid black;
}

#red {
  background-color: #a33131;
  border-left:10px solid black;
  border-bottom:10px solid black;
}
#yellow {
  background-color: #c7bd40;
  border-right:10px solid black;
  border-top:10px solid black;
}
#blue {
  background-color: #34b1d5;
  border-left:10px solid black;
  border-top:10px solid black;
}

#middle {
  position: relative;
  background-color: #cfcfcf;
  border: 10px solid black;
  width: 250px;
  height: 250px;
  border-radius: 50%;
  -moz-border-radius: 50%;
  -webkit-border-radius: 50%;
  margin: 105px auto;
  text-align: center;
}
#middle h1 {
  position: absolute;
  font-size: 8em;
  margin: 40px 28px 0 28px;
}

#middle #items-div{
  position: absolute;
  margin: 120px 33px 0px 33px;
  /*background-color: white;*/
  font-size: 2em;
}

#middle #items-div div{
  display: inline;
  float: left;
  width: 50px;
  height: 50px;
  /*background-color: green;*/
}
#count-div{
  margin-right: 5px;
}

#count-div #count {
  width: 50px;
  height: 30px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border: 3px solid black;
  margin: 0;
  background-color: #31060d;
  /* Used photoshop to get the exact same colors*/
}

#count-div .off{
  color: purple;
}
#count-div .on{
  color: #da1430;
}
#start-div{
  margin-right: 5px;
}
#start-div #start{
  width: 30px;
  height: 30px;
  border-radius: 50%;
  -moz-border-radius: 50%;
  -webkit-border-radius: 50%;
  background-color: red;
  box-shadow: 0px 0px 5px black;
  border: 3px solid black;
}
#start-div #start:active{
  box-shadow: none;
}

#strict-div #strict{
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 3px solid black;
  -moz-border-radius: 50%;
  -webkit-border-radius: 50%;
  background-color: yellow;
  box-shadow: 0px 0px 5px black;
}
#strict-indicator {
  width: 10px;
  height: 10px;
  background-color: #31060d;
  border-radius: 50%;
  -moz-border-radius: 50%;
  -webkit-border-radius: 50%;
  border: 2px solid black;
  position: relative;
  float: right;
}
#strict-div .on {
  background-color: #da1430;
}
#strict-div #strict:active{
  box-shadow: none;
}

#switch {
  postion: absolute;
  width: 45px;
  height: 20px;
  background-color: black;
  margin: 180px 90px 0px 90px;
  border-radius: 2px;
  -moz-border-radius: 2px;
  -webkit-border-radius: 2px;
}

#slider {
  position: relative;
  width: 16px;
  height: 16px;
  background-color: #3895db;
  border-radius: 2px;
  -moz-border-radius: 2px;
  -webkit-border-radius: 2px;
  top: 2px;
}

#slider:hover {
  cursor: pointer;
}

#switch .off {
  left: 2px;
}

#switch .on {
  left: 27px;
}
/*@media only screen and (max-width: 550px) {
#simon {
  background-color: gray;
  width: 350px;
  height: 350px;
  border: solid black 10px;
}

#top, #bottom {
  width: 350px;
}

#top div, #bottom div {
  width: 165px;
  height: 165px;
}

#green {
  border-right:5px solid black;
  border-bottom:5px solid black;
}

#red {
  border-left:5px solid black;
  border-bottom:5px solid black;
}
#yellow {
  border-right:5px solid black;
  border-top:5px solid black;
}
#blue {
  border-left:5px solid black;
  border-top:5px solid black;
}

#middle {
  border: 5px solid black;
  width: 175px;
  height: 175px;
  margin: 77.5px auto;
}
#middle h1 {
  font-size: 4em;
  margin: 10px 42.9px 0 42.9px;
}

#middle #items-div{
  margin: 50px;
  font-size: 1.5em;
}

#middle #items-div div{
  width: 35px;
  height: 35px;
}
#count-div{
}

#count-div #count {
  width: 35px;
  height: 20px;
  border-radius: 2.5px;
  -moz-border-radius: 2.5px;
  -webkit-border-radius: 2.5px;
  border: 1.8px solid black;
}
#start-div{
}
#start-div #start{
  width: 15px;
  height: 15px;
  border: 1.8px solid black;
}

#strict-div #strict{
  width: 15px;
  height: 15px;
  border: 1.8px solid black;
}
#strict-indicator {
  width: 5px;
  height: 5px;
  border: 1.3px solid black;
}
#switch {
  width: 45px;
  height: 20px;
  background-color: black;
  margin: 180px 90px 0px 90px;
  border-radius: 2px;
  -moz-border-radius: 2px;
  -webkit-border-radius: 2px;
}

#slider {
  position: relative;
  width: 16px;
  height: 16px;
  background-color: #3895db;
  border-radius: 2px;
  -moz-border-radius: 2px;
  -webkit-border-radius: 2px;
  top: 2px;
}

#slider:hover {
  cursor: pointer;
}

#switch .off {
  left: 2px;
}

#switch .on {
  left: 27px;
}
}
*/
/*TODO: fix small screen sizes*/
/*need to change width limit too*/
/*so far works until items div*/
            
          
!
            
              $(document).ready(function() {
  // Boolean is simon on/off
  var isSimonOn = false;
  // Elements that turn on/off
  var htmlElements = ["#count", "#slider"];
  var colors = ["green", "red", "yellow", "blue"];
  var sounds = {greenSound: new Howl({urls: ["https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"], volume: 1}),
                redSound: new Howl({urls: ["https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"], volume: 1}),
                yellowSound: new Howl({urls: ["https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"], volume: 1}),
                blueSound: new Howl({urls: ["https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"], volume: 1}),
                winSound: new Howl({urls: ["https://res.cloudinary.com/santiago-g-mez/video/upload/v1468106293/Ta_Da-SoundBible.com-1884170640_mkltoe.mp3"], volume: 0.5}),
                looseSound: new Howl({urls: ["https://res.cloudinary.com/santiago-g-mez/video/upload/v1468106294/fail-trombone-02_pwswzz.mp3"]}),
                onSound: new Howl({urls: ["https://res.cloudinary.com/santiago-g-mez/video/upload/v1468106651/Magic_Wand_Noise-SoundBible.com-375928671_e4kksm.mp3"], volume: 0.3}),
                offSound: new Howl({urls: ["https://res.cloudinary.com/santiago-g-mez/video/upload/v1468106647/Crowd_Boo_1-SoundBible.com-183064743_n54ame.mp3"], volume: 0.3}),
               errorSound: new Howl({urls: ["https://res.cloudinary.com/santiago-g-mez/video/upload/v1468349333/Buzz-SoundBible.com-1790490578_oufb5l.wav"], sprite: {error: [0, 250]}})};
 
  // Random sequence
  var sequence = [];
  // Next to play 0 AI/ 1 User.
  var turn ;
  // Number of guess
  var playNumber;
  var strict;
  var userSequence = [];
  
  
  // Function generates a random sequence.
  function generateSequence(){
    sequence = [];
    while (sequence.length < 20) {
      sequence.push(colors[Math.floor(Math.random() * 4)]);
    }
  }
  
  // Function shows color illuminate and displays its sound.
  function showColor(color) {
    // Makes pushed div brighter.
    $("#" + color).css("filter", "brightness(200%)");
    sounds[color + "Sound"].play();
    setTimeout(function() {
      // Makes pushed div original bright.
      $("#" + color).css("filter", "brightness(100%)");
    }, 1000);
  }
  
  // Function shows random sequence until the current playNumber.
  function displaySequence() {
    var i = 0;
    function showSequence(){
      if (i <= playNumber && isSimonOn) {
      showColor(sequence[i]);
      setTimeout(showSequence, 1000);
      i++;
      }
      else {
        // Finished displaying, now user can play.
        turn = 1;
      }
    }
    showSequence();
}
  
  function updateCounter() {
    $("#count").text(playNumber < 9 ? "0" + (playNumber + 1): playNumber + 1);
  }
  
  function userPlay(element) {
    userSequence.push(element);
    for (var i = 0; i < userSequence.length; i++) {
      if (userSequence[i] !== sequence[i]) {
        if (strict) {
          turn = 0;
          sounds["looseSound"].play();
          setTimeout(function(){
            newGame();
          }, 4500);  
        }
        else {
          sounds["errorSound"].play("error");
          turn = 0;
          userSequence = [];
          setTimeout(function (){
            displaySequence();
          }, 2000);
        }
      }
    }
    if (userSequence.length === playNumber + 1) {
      if (playNumber < 19) {
        playNumber++;
        updateCounter();
        turn = 0;
        userSequence = [];
        setTimeout(function(){
          displaySequence();
        }, 2000);
      }
      else {
        sounds["winSound"].play();
        newGame();
      }
    }
  }
  
  function newGame() {
    userSequence = [];
    playNumber = 0;
    turn = 0;
    generateSequence();
    updateCounter();
    displaySequence();
    $("#green, #red, #yellow, #blue ").unbind(); // "What's happening is that your #green, #yellow, ... was already attached to the "click" event ! And you're attaching to the "click" event when you lose the game in strict mode. So when you click just one time on your tile, it triggers, in fact many clicks (for the computer) ! So the sequence is wrong  Got it ?"- @thomlom 
    $("#green, #red, #yellow, #blue ").on("click", function (){
      if (turn === 1) {
        showColor(this.id);
        userPlay(this.id);
      }
    }); 
  }

  // Function to turn Simon on/off
  $("#slider").on("click", function() {
    if ($(this).hasClass("on")) {
     for (var element = 0; element < htmlElements.length; element++) {
       console.log(htmlElements[element]);
       $(htmlElements[element]).removeClass("on");
       $(htmlElements[element]).addClass("off");
      
     }
      isSimonOn = false;
      $("#count").text("--");
       sounds["offSound"].play();
       $("#strict-indicator").removeClass("on");
       $("#strict-indicator").addClass("off");
    }
    else {
      for (element = 0; element < htmlElements.length; element++) {
       $(htmlElements[element]).removeClass("off");
       $(htmlElements[element]).addClass("on");
     }
      isSimonOn = true;
       sounds["onSound"].play();
    }  
  });
  
  // Function (des)activates strict mode.
  $("#strict").on("click", function() {
    if (isSimonOn) {
      if ($("#strict-indicator").hasClass("on")) {
        $("#strict-indicator").removeClass("on");
        $("#strict-indicator").addClass("off");
        strict = false;
      }
      else {
        $("#strict-indicator").removeClass("off");
        $("#strict-indicator").addClass("on");
        strict = true;
      }
    }
  });
  // Function starts new game.
  $("#start").on("click", function(){
    if (isSimonOn) {
      newGame();
    }
  });
})
            
          
!
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.

Console