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 class="game">
<div class="container">
<audio class="green">
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"></source>
</audio>
<audio class="red">
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"></source>
</audio>
<audio class="yellow">
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"></source>
</audio>
<audio class="blue">
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"></source>
</audio>


<button class="simon green" id="1"></button>
<button class="simon red" id="2"></button>
<button class="simon yellow" id="3"></button>
<button class="simon blue" id="4"></button>

<div class="ui">
<div class="title">Simon</div>
<p>Count</p>
<p id="score">0</p>
<button class="functions" id="start">Start</button>
<button class="functions" id="strict">Strict</button> 
</div>

</div>
<div>
</body>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Share+Tech+Mono');
@import url('https://fonts.googleapis.com/css?family=Alfa+Slab+One');

body {
 background-image: url('http://cdn.backgroundhost.com/backgrounds/subtlepatterns/retina_wood.png');
}

p {
  width: 70px;
  font-size: 30px;
  font-family: 'Share Tech Mono', monospace;
  margin: 3% 0 0 35%;
}

.container {
  margin: 0 auto;
  padding: 0;
  text-align: center;
}

.game {
  margin: 0 auto;
  margin-top: 50px;
  width: 600px;
  height: 600px;
  border: solid 7px #232323;
  background-color: #232323;
  border-radius: 600px;
}

.title {
  font-family: 'Alfa Slab One', cursive;
  margin-top: 8%;
  margin-left: 5%;
  font-size: 60px;
  color: black;
}

.functions {
  margin-top: 5%;
  width: 80px;
  height: 40px;
  outline: none;
}

.simon {
  height: 293px;
  width: 293px;
  margin: 0 auto;
  float: left;
  position: relative;
  border: solid 5px #232323;
  outline: none;
}

.ui {
  text-align: center;
  margin-left: 140px;
  margin-top: 140px;
  width: 300px;
  height: 300px;
  position: absolute;
  background-color: white;
  border: solid 7px #232323;
  border-radius: 300px;
}

.green {
  border-radius: 300px 0 0 0;
  background-color: green;
}

.green:active {
  background-color: #5cdb5c;
}

.greenTurn {
  background-color: #5cdb5c;
}

.red {
  border-radius: 0 300px 0 0;
  background-color: red;
}

.red:active {
  background-color: #e52d2d;
}

.redTurn {
  background-color: #e52d2d;
}

.yellow {
  border-radius: 0 0 0 300px;
  background-color: yellow;
}

.yellow:active {
  background-color: #e5e590;
}

.yellowTurn {
  background-color: #e5e590;
}

.blue {
  border-radius: 0 0 300px 0;
  background-color: blue;
}

.blue:active {
  background-color: #5050f4;
}

.blueTurn {
  background-color: #5050f4;
}

.on {
  background-color: green;
}
            
          
!
            
              var start = $("#start"), strict = $("#strict"), green = $("#1"), red = $("#2"), yellow = $("#3"), blue = $("#4"), count = [], ai = [], score = $("#score"), difficulty = 1;

$("body").ready(function(){
  disableButtons();
})

//starts counter from 1
start.click(function(){
  ai = [];
  ai.push(Math.floor((Math.random() * 4) + 1));
  enableButtons();
  score.text(ai.length);
  colorsAnimation();
})

strict.click(function(){
  difficulty += 1;
  if(difficulty % 2 == 0){
    strict.addClass("on");
  } else if (difficulty % 2 == 1){
    strict.removeClass("on");
  }
})

function disableButtons () {
  green.attr("disabled", true);
    red.attr("disabled", true);
    yellow.attr("disabled", true);
    blue.attr("disabled", true);
}

function enableButtons () {
  green.prop("disabled", false);
    red.prop("disabled", false);
    yellow.prop("disabled", false);
    blue.prop("disabled", false);
}

function colorsAnimation () {
  let intervalID = setTimeout(function(){    
  for(var i = 0; i < ai.length; i++){
    let delay = i * 1100;
    let out = delay + 800;
    let enabler = ai.length * 1000;
    if(ai[i] === 1){
      setTimeout(function(){let audio = $(".green")[0];
audio.play();}, delay);
      setTimeout(function(){green.addClass("greenTurn");}, delay);
      setTimeout(function(){green.removeClass("greenTurn");}, out);
    } else if(ai[i] === 2){
      setTimeout(function(){let audio = $(".red")[0];
audio.play();}, delay);
      setTimeout(function(){red.addClass("redTurn");}, delay);
      setTimeout(function(){red.removeClass("redTurn");}, out);
  } else if(ai[i] === 3){
    setTimeout(function(){let audio = $(".yellow")[0];
audio.play();}, delay);
     setTimeout(function(){yellow.addClass("yellowTurn");}, delay);
      setTimeout(function(){yellow.removeClass("yellowTurn");}, out);
} else if(ai[i] === 4){
  setTimeout(function(){let audio = $(".blue")[0];
audio.play();}, delay);
      setTimeout(function(){blue.addClass("blueTurn");}, delay);
      setTimeout(function(){blue.removeClass("blueTurn");}, out);
}
    setTimeout(function(){enableButtons()}, enabler);
  }
  score.text(ai.length); 
  }, 1000);
}

function compare () {
  let space = count.length - 1;
  let userIdx = count[space];
  let aiIdx = ai[space];
//compares each number until it reaches a lenght of 20 to finish game
  if(count.length === ai.length && userIdx === aiIdx && count.length === 20){
    ai.push(Math.floor((Math.random() * 4) + 1));
    alert("You Won The Game!");
    count = [];
    console.log(ai);
    disableButtons();
//checks the lenght of both arrays and if the elements are matching
  } else if (count.length === ai.length && userIdx === aiIdx) {
    disableButtons();
    ai.push(Math.floor((Math.random() * 4) + 1));
    count = [];
    setTimeout(function(){colorsAnimation()}, 700);
//checks if the last element added matches the same index in the ai's array
  } else if (difficulty % 2 == 0 && userIdx !== aiIdx){
    disableButtons();
    score.text("!!");
    ai = [];
    count = [];
    ai.push(Math.floor((Math.random() * 4) + 1));
    setTimeout(function(){colorsAnimation()}, 700);
  } else if (userIdx !== aiIdx){
    disableButtons();
    score.text("!!");
    count = [];
    setTimeout(function(){colorsAnimation()}, 700);
  }
}

green.click(function(){
  count.push(1);
  compare();
  var audio = $(".green")[0];
audio.play();
})

red.click(function(){
  count.push(2);
  compare();
  var audio = $(".red")[0];
audio.play();
})

yellow.click(function(){
  count.push(3);
  compare();
  var audio = $(".yellow")[0];
audio.play();
})

blue.click(function(){
  count.push(4);
  compare();
  var audio = $(".blue")[0];
audio.play();
})
            
          
!
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