Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="main-wrapper">
  
  <div class="row">
    <div class="button" id="btn1" value="1"></div>
    <div class="button" id="btn2" value="2"></div>
  </div>
  <div class="row">
    <div class="button" id="btn3" value="3"></div>
    <div class="button" id="btn4" value="4"></div>
  </div>
  
  <div id="switch-wrapper">
    <div class="s-row">
      <div id="counter-wrapper">
        <div id="counter"></div>
      </div>
    </div>
    
    <div class="s-row">
      <div class="r-btn-wrapper">
        <button class="raised-button" id="start"></button>
        <span>START</span>
      </div> 
      <div class="r-btn-wrapper">
        <div class="round">
          <input type="checkbox" id="strict" disabled/>
          <label for="strict"></label>
        </div> 
        <span id="strict">STRICT</span>
      </div> 
    </div>
    
    <div class="s-row">
      <span>OFF</span>
      <label class="switch">
        <input type="checkbox" id="power">
        <span class="slider"></span>
      </label>
      <span>ON</span>
    </div>
    
  </div>

</div>
              
            
!

CSS

              
                body{
  font-family:sans-serif;
  background-color:#cccccc;
}

#main-wrapper {
  width : 400px;
  height : 400px;
  background-color:red;
  position:relative;
  margin:auto;
  margin-top:5%;
  border:10px solid black;
  border-radius: 5%;
  box-shadow: 0 1px 8px 10px rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.row {
  width:100%;
  height:50%;
  position:absolute;
}

.row:nth-child(1) {/*green and red buttons*/
  top:0;
}

.row:nth-child(2) {/*yellow and blue buttons*/
  bottom:0;
}

.button {
  width: 50%;
  height:100%;
  position:absolute;
  cursor:pointer;
  pointer-events:none;
}

.button:nth-child(2) {
  right:0;
}

.button:nth-child(1) {
  left:0;
}

#btn1 {
  background-color:#1b5e20;
}
.btn1:active{
  background-color:#00e676 !important;
}

#btn2{
  background-color:#b71c1c;
}
.btn2:active{
  background-color:#ff1744 !important;
}

#btn3{
  background-color:#f9a825;
}
.btn3:active{
  background-color:#ffeb3b !important;
}

#btn4{
  background-color:#0d47a1;
}
.btn4:active {
  background-color:#448aff !important;
}

#switch-wrapper {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  background-color: #333333;
  width:50%;
  height:50%;
  text-align:center;
  top: 25%;
  left: 25%;
  position:absolute;
  border-radius: 10%;
  color:#fff;
}

.s-row {
  text-align:center;
  margin:auto;
  width:100%;
  height: 33.33%;
  position:absolute;
  font-weight:bold;
}

.s-row:nth-child(1){ /*Counter*/
  top:0;
}

.s-row:nth-child(2){ /*start and strict buttons*/
  top:33%;
}

.s-row:nth-child(3){ /*power switch*/
  bottom:0;
}

.s-row:nth-child(3) span:nth-child(1){ /*OFF*/
  position:relative;
  top:12.5%;
}

.s-row:nth-child(3) span:nth-child(3){/*ON*/
  position:relative;
  top:12.5%;
}

#counter-wrapper{
  background-color:#fff;
  color:white;
  height: 40%;
  width:50%;
  font-size:1.5em;
  border:5px solid #000;
  text-align:center;
  position:absolute;
  top:33%;
  left:23%;
  color:red;
}

#counter{
  line-height:120%;
  margin:auto;
}


/*power switch*/
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
  top:25%;
}

.switch input {display:none;}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}
/*end power switch*/

/*start button*/
.raised-button {
  display: inline-block;
  padding: 10px 10px;
  font-size: 5px;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  outline: none;
  color: #fff;
  background-color: #990000;
  border: 5px solid black;
  border-radius: 50%;
}

.raised-button:active {
  /* background-color:red; */
  transform: translateY(4px);
}
/*end start button*/

.r-btn-wrapper{
  width:50%;
  height:100%;
  position:absolute;
  font-weight:bold;
} /*start and strict buttons wrapper*/
.r-btn-wrapper:nth-child(1){
  left:0; 
}/*START button wrapper*/
.r-btn-wrapper:nth-child(2){
  right:0;
}/*STRICT button wrapper*/
.r-btn-wrapper:nth-child(1) button{
  position:absolute;
  top:10%;
  left:35%;
} /*START button*/
.r-btn-wrapper:nth-child(1) span{
  position:absolute;
  bottom:10%;
  left:27%;
}
.r-btn-wrapper:nth-child(2) span{
  position:absolute;
  bottom:10%;
  left:25%;
}
.r-btn-wrapper:nth-child(2) .round{
  position:absolute;
  top:10%;
  left:35%;
}/*STRICT button*/

/* rounded checkbox */
.round {
  position: relative;
  top:25%;
}

.round:active {
  transform: translateY(4px);
}

.round label {
  background-color: #999900;
  border: 5px solid black;
  border-radius: 50%;
  cursor: pointer;
  height: 20px;
  left: 0;
  position: absolute;
  top: 0;
  width: 20px;
}

.round label:after {
  border-top: none;
  border-right: none;
  content: "";
  height: 6px;
  left: 7px;
  opacity: 0;
  position: absolute;
  top: 8px;
  transform: rotate(-45deg);
  width: 12px;
}

.round input[type="checkbox"] {
  visibility: hidden;
}

.round input[type="checkbox"]:checked + label {
  background-color: yellow;
}

.round input[type="checkbox"]:checked + label:after {
  opacity: 1;
}
/* end rounded checkbox */
              
            
!

JS

              
                $("document").ready(function(){
  var counterText = $("#counter");
  var startButton = $("#start");
  var strictMode = $("#strict");
  var powerButton = $("#power");
  
  var green = $("#btn1");
  var red =  $("#btn2");
  var yellow = $("#btn3");
  var blue = $("#btn4");
  
  var isPowerOn = false;
  var isStrict = false;
  
  var audio = [];
  var sounds = ["https://s3.amazonaws.com/freecodecamp/simonSound1.mp3",        "https://s3.amazonaws.com/freecodecamp/simonSound2.mp3",    "https://s3.amazonaws.com/freecodecamp/simonSound3.mp3",  "https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"];
  for(var i=0; i<sounds.length; i++){
    audio[i] = new Audio(sounds[i]);
  }  
  
  var steps = [];
  var stepCounter = 0; 
  
    
  powerButton.change(function(){
    if($(this).is(":checked")){
      counterText.text("--"); 

      startButton.click(function(){
        clearInterval(interval);
        releaseButtons();
        $(this).css("background-color","red");
        steps = [];
        steps.push(getRandom());//push a random step into the steps array
        play();//play the random sounds
      });
      
      strictMode.attr("disabled",false);
      strictMode.change(function(){
        if($(this).is(":checked")){
          isStrict = true;
        } else {
          isStrict = false;
        }
      });
      
    } else { 
      counterText.text("");
      startButton.css("background-color","#990000");
      startButton.off();
      strictMode.attr("disabled",true);
      strictMode.prop('checked', false);
      strictMode.off();
      clearTheGame();
    }
    
  });
  
  function clearTheGame(){
      steps = [];
      turnOffButtons();
      releaseButtons();
      clearInterval(interval);
  }
  
  function turnOnButtons(){
    var userClicks = 0;
    
    for(var i=1; i<=4; i++){
      $("#btn"+i).addClass("btn"+i);//activate buttons
      (function(i){
        
        $("#btn"+i).mousedown(function(){//mouse goes down 
          audio[i-1].play();  // play the audio
        });
        
        $("#btn"+i).mouseup(function(){//mouse goes up
          if(steps[userClicks] === i) {//to check if the current key matches the steps
            userClicks++;//increase the click counter
            if (userClicks === steps.length){ // if last click
              if(steps.length === 20){ // Won the game
                turnOffButtons();
                clearTheGame();
                alert("Congratulations You Win!");
              }
              steps.push(getRandom());//push a random step into the steps array
              play(); // replay the steps with an additional step
            }
          } else {//if wrong key pressed
            turnOffButtons();
            if (isStrict) { // if strict mode is on
              steps = []; //clear all the steps
              steps.push(getRandom());//push one random step
            }
            counterText.text("!!").fadeTo(250, 0).fadeTo(250,1).fadeTo(250,0).fadeTo(250,1);
            setTimeout(function(){
              play();// replay the game
            }, 1000);
            
          }
        });
        
      }(i));
    }
    
    $(".button").css("pointer-events","all");//enable pointer events
  }
  
  function turnOffButtons(){
    for(var i=1; i<=4; i++){
      $("#btn"+i).off();
      $("#btn"+i).removeClass("btn"+i);
    }
    $(".button").css("pointer-events","none");
  }
  
  function clickButton(btn){
    switch (btn) {
      case "#btn1":
        $(btn).css("background-color","#00e676");
        audio[0].play();
        break;
      case "#btn2":
        $(btn).css("background-color","#ff1744");
        audio[1].play();
        break;
      case "#btn3":
        $(btn).css("background-color","#ffeb3b");
        audio[2].play();
        break;
      case "#btn4":
        $(btn).css("background-color","#448aff");
        audio[3].play();
        break;
      default:
        break;
    }
  }
  
  function releaseButton(btn){
    switch(btn){
      case "#btn1":
        $(btn).css("background-color","#1b5e20");
        break;
      case "#btn2":
        $(btn).css("background-color","#b71c1c");
        break;
      case "#btn3":
        $(btn).css("background-color","#f9a825");
        break;
      case "#btn4":
        $(btn).css("background-color","#0d47a1");
        break;
      default: 
        break;
    }
  }    
  
  function releaseButtons(){
    for(var i=1; i<=4; i++){
        releaseButton("#btn"+i);
    }
  }
  
  var interval;
  function play(){ 
    counterText.text(("0"+steps.length).slice(-2)).fadeTo(250, 0).fadeTo(250,1);
    turnOffButtons();//prevent user clicks while playing
    stepCounter = 0; //start the steps from 0 up to the steps length
    interval = setInterval(function(){//repeat this every 1000 ms
      releaseButton("#btn" + steps[stepCounter - 1]);// Release the previous button
      clickButton("#btn" + steps[stepCounter]);// click the current button
 
      if(stepCounter === steps.length){// if last step reached
        clearInterval(interval);// stop the interval clock
        turnOnButtons(); //let the user click buttons
        stepCounter = 0;//reset steps
      } 
      
      stepCounter++;// increase step counter
    }, 1000);
  }
  
  function getRandom(){
    var random = Math.floor(Math.random() * 4) + 1;
    while(random === steps[steps.length - 1]){
      random = Math.floor(Math.random() * 4) + 1;
    }
    return random;
  }
  
});
              
            
!
999px

Console