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.

            
              <link href="https://fonts.googleapis.com/css?family=Roboto+Slab:700" rel="stylesheet">
<div id="container">
  <div id="controls">
    <h1>Simon&reg;</h1>
    <div id="score">--</div>
      <div class="start"><button class="btn btn-start"></button>Start</div>
      <div class="strict"><button class="btn btn-strict"></button>Strict</div>
  </div>
  <div id="colors">
    <article id="green"></article>
    <article id="red"></article>
    <article id="yellow"></article>
    <article id="blue"></article>
  </div>
</div>

            
          
!
            
              $red: #FF0200
$yellow: #FFCC00
$green: #0890A6
$blue: #003399
$white: #FEFEFE
$black: #000

body
  font-size: 16px
  padding: 0
  margin: 0
  display: flex
  height: 100vh
  align-items: center
  justify-content: center
  background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/176972/vintage-concrete.png')
  background-color: $white
.btn
  border-radius: 100%
  width: 5px
  height: 5px
  text-align: center
  padding-top: 10px
  margin: 3px
.start
  width: 50%
  .btn
    background-color: $yellow
.strict
  width: 50%
  .btn
    background-color: $yellow
#colors
  display: flex
  width: 350px
  height: 350px
  margin: auto
  border-radius: 50%
  flex-wrap: wrap
#container
  position: relative
  font-family: 'Roboto Slab', serif
  font-size: 2em
  width: 500px
  margin: auto
  display: flex
  justify-content: center
  align-items: center
#controls
  position: absolute
  display: flex
  width: 140px
  height: 140px
  top: calc(50% - 80px)
  background-color: $white
  flex-wrap: wrap
  line-height: 0
  font-size: .5em
  border-radius: 50%
  border: 10px solid $black
  padding: 25px
  h1  
    font-size: 1.5em
    width: 100%
    text-align: center
  #score
    background-color: $red
    padding:  15px 0 10px 
    width: 10px
    height: 10px
    width: 100%
    margin-bottom: 15px
    text-align: center
article
  overflow: hidden
  width: calc(50% - 20px)
  height: 50%
.green-lighten
  background-color: lighten($green, 25%) !important
.red-lighten
  background-color: lighten($red, 25%) !important
.yellow-lighten
  background-color: lighten($yellow, 25%) !important
.blue-lighten
  background-color: lighten($blue, 25%) !important
#green
  background-color: $green
  border: 10px solid $black
  border-radius: 200px 0 0 0
  filter: alpha(opacity=50)
#red
  background-color: $red
  border: 10px solid $black
  border-radius: 0 250px 0 0
#yellow
  background-color: $yellow
  border: 10px solid $black
  border-radius: 0 0 0 250px
#blue
  background-color: $blue
  border: 10px solid $black
  border-radius: 0 0 250px 0
heading
  top: 0
  left: 0
  margin: auto
            
          
!
            
              let BUTTONS_RANDOM = [];
let BUTTONS = [];
const COLORS = [green, red, yellow, blue];
let SCORE = 0; 
let STRICT =  false;
let START = false;
//buttons
const START_BUTTON = document.querySelector(".btn-start");
const STRICT_BUTTON = document.querySelector(".btn-strict")
//colors 
const GREEN = document.getElementById("green");
const RED = document.getElementById("red");
const YELLOW = document.getElementById("yellow");
const BLUE = document.getElementById("blue");
const ALL_COLORS = document.querySelectorAll("#colors > article");
let PLAY_INTERVAL=1000;

START_BUTTON.addEventListener("click", startGame);

STRICT_BUTTON.addEventListener("click", strictClick);

//.addEventListener("click", checkColors);
ALL_COLORS.forEach(ALL_COLORS => ALL_COLORS.addEventListener("click", checkColors));

const AUDIO_GREEN=new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
const AUDIO_RED=new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
const AUDIO_YELLOW=new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
const AUDIO_BLUE=new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
const AUDIO_WRONG=new Audio('https://s3-us-west-2.amazonaws.com/s.cdpn.io/176972/331912__kevinvg207__wrong-buzzer.mp3');

let COUNTER_RIGHT=0;

function startGame(){
  
  if(START===false){
    document.getElementById("score").innerHTML=SCORE;
    START_BUTTON.style.background = "#FF0200";
    START=true;
    
    randomNumbers(true);
  }else{
    SCORE=0;
    COUNTER_RIGHT=0;
    document.getElementById("score").innerHTML="--";
    START_BUTTON.style.background = "#FFCC00";
    START=false;    
    BUTTONS=[];
    BUTTONS_RANDOM=[];
  }
  
}

function strictClick(){
  if(STRICT===false){
    STRICT_BUTTON.style.background = "#FF0200";
    STRICT=true;
  }else{
    STRICT_BUTTON.style.background = "#FFCC00";
    STRICT=false;     
  }

}

function checkButtons(){ 
    if(BUTTONS[COUNTER_RIGHT]===BUTTONS_RANDOM[COUNTER_RIGHT]){
        COUNTER_RIGHT++;        
        randomNumbers(true);
      }else{
        randomNumbers(false);
        AUDIO_WRONG.play();
      } 
    
}

function checkColors(){
  if(START){
    if(this.id==="green"){
      playGreen();
      BUTTONS.push(1);
    }else if(this.id==="red"){
      playRed();
      BUTTONS.push(2);
    }else if(this.id==="yellow"){
      playYellow();
      BUTTONS.push(3);
    }else{
      playBlue();
      BUTTONS.push(4);
    }    
    
    //if(BUTTONS.length>=BUTTONS_RANDOM.length){
      checkButtons();
    //}
  }
}

function randomNumbers(correctColors){
  //BUTTONS=[];
  if(correctColors){
    //console.log(COUNTER_RIGHT, BUTTONS.length);
    if(COUNTER_RIGHT===20){
      youWin();
    }else if(COUNTER_RIGHT===BUTTONS_RANDOM.length){
      BUTTONS=[];
      BUTTONS_RANDOM.push(Math.floor(Math.random() * (5 - 1)) + 1);
      if(score!==0){
        SCORE++;  
      }
      document.getElementById("score").innerHTML=SCORE;      COUNTER_RIGHT=0;
     playColors();
    }
  }else{
    BUTTONS=[];
    COUNTER_RIGHT=0;
    if(STRICT===true){
      BUTTONS_RANDOM=[];
      SCORE=0;
      BUTTONS_RANDOM.push(Math.floor(Math.random() * (5 - 1)) + 1);
    }
    playColors();
  }
  
  //console.log(BUTTONS_RANDOM, BUTTONS, COUNTER_RIGHT);
}

function youWin(){
  BUTTONS_RANDOM=[];
  SCORE=0;
  BUTTONS=[];
  STRICT_BUTTON.style.background = "#FFCC00";
  STRICT=false;
  COUNTER_RIGHT=0;
  document.getElementById("score").innerHTML="You Won!";
  START_BUTTON.style.background = "#FFCC00";
  START=false;
}

function playColors(){
  let interval=0;
  
    for(var i=0; i<=BUTTONS_RANDOM.length; i++){

      interval+=1300;

      if(BUTTONS_RANDOM.length>1){
         if(BUTTONS_RANDOM[i]===BUTTONS_RANDOM[i-1]){
            PLAY_INTERVAL+=interval;
         }else{
           PLAY_INTERVAL=1000;
         }
      }
  
      if(BUTTONS_RANDOM[i]===1){
        setTimeout(playGreen, interval);
      }else if(BUTTONS_RANDOM[i]===2){
        setTimeout(playRed, interval);
      }else if(BUTTONS_RANDOM[i]===3){
        setTimeout(playYellow, interval);
      }else if(BUTTONS_RANDOM[i]===4){
        setTimeout(playBlue, interval);
      }else{
      
      }
    }
}

function playGreen(){
  GREEN.classList.add("green-lighten");
  AUDIO_GREEN.play();
  setTimeout(function(){
    GREEN.classList.remove("green-lighten");
  }, PLAY_INTERVAL);  
}

function playRed(){
  RED.classList.add("red-lighten");
  AUDIO_RED.play();
  setTimeout(function(){
    RED.classList.remove("red-lighten");
  }, PLAY_INTERVAL);  
}

function playYellow(){
  YELLOW.classList.add("yellow-lighten");
  AUDIO_YELLOW.play();
  setTimeout(function(){
    YELLOW.classList.remove("yellow-lighten");
  }, PLAY_INTERVAL);  
}

function playBlue(){
  BLUE.classList.add("blue-lighten");
  AUDIO_BLUE.play();
  setTimeout(function(){
    BLUE.classList.remove("blue-lighten");
  }, PLAY_INTERVAL);  
}
            
          
!
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