cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <div id="container">
<h1>Test Your Memory</h1>
 
  <div class="buttonHolder">
    <div id="buttons">
      <button id="reset">Reset</button>
      <button id="start">Start</button>
      <button id="strict">Mode</button>
      </div>
    
     <div class="buttonHolder">
    <button2 id="steps" type="button">Count </button2>
    <!--<div class="button" id="steps">-->
     </div>   
    </div>
 <div id="game">
   <div>
      <div id="red">
      </div>
      <div id="green">
      </div>
      <div>
      <div id="blue">
      </div>
      <div id="yellow">
      </div>
    </div>
</div>
</div>
<h1>Simon in Pastel</h1>
</div>
            
          
!
            
              /* globals */
h1 {
  color: #fff;
  text-align: center;
  font-family: 'arial', tahoma;
}

button {
  width: 10%;
  padding: 10px 0px;
  background-color: #d8d4d4;
  color: red;
  font-weight: bold;
  cursor: pointer;
}

button2 {
  width: 50%;
  padding: 10px 20px;
  background-color: #fff;
  color: #000;
  font-weight: bold;
  font-size: x-large;
  border: 3px solid #000;
  cursor: pointer;
}

button:hover {
  background-color: #ababab;
}

#container {
  background-color: #5b5353;
}

.buttonHolder{ 
text-align: center;
margin-top: 25px;
margin-bottom: 25px;
}

#game {
  width: 320px;
  margin: 0 auto;
  /*position: relative;*/
}

/* color tiles */
#red,#green,#yellow,#blue {
  position: relative;
  display: inline-block;
  width: 140px;
  height: 140px;
  border: 3px solid #000;
}
#red {
  background-color: #F49AC2;
}
#green {
  background-color: #77DD77;
}
#yellow {
  background-color:#FDFD96;
}
#blue {
  background-color: #779ECB;
}


            
          
!
            
              //cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js

//variables
var colorArray = $("#red, #green, #blue, #yellow").toArray();
var randomArray;
var stepArray;
var playerArray = [];
var step = 0;
var strict = false;
var sounds = {
  "red": new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"),
  "green": new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"),
  "blue": new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"),
  "yellow": new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3")
};

//js Math object allows you to perform mathematical tasks on numbers.
function makeRandomArray() {
  function math() {
    return Math.floor((Math.random() * 4) + 1);
  }
  var r = Array.apply(null, {
    length: 20
    //The map() method creates a new array with the results of calling a provided function on every element in this array.
  }).map(Function.call, math);
  var rA = [];
  for (var i = 0; i < r.length; i++) {
    rA.push(colorArray[r[i] - 1]);
  }
  return rA;
}

function updateStep(num) {
  step = num;
  $("#steps").html(step);
}

function newGame() {
  strict = strict;
  updateStep(0);
  randomArray = makeRandomArray();
  playAndAnimate();
}
//The every() method tests whether all elements in the array pass the test implemented by the provided function.
function is_same() {
  return playerArray.every(function(element, index) {
    return element === stepArray[index];
  });
}

function playAndAnimate() {
  playerArray = [];
  stepArray = randomArray.slice(0, step + 1); 
  function loopThroughStepArray(stepArray) {
    displayValue(stepArray, 0);
  }

  function displayValue(arr, i) {
    if (i < arr.length) {
      setTimeout(function() {
        sounds[arr[i].id].play();
        $(arr[i]).fadeTo('fast', 0.33).fadeTo('fast', 1);
        displayValue(arr, i + 1);
      }, 1200);  //fadeTo() method gradually changes the opacity, for selected elements, to a specified opacity (fading effect).
    }
  }
  loopThroughStepArray(stepArray);
  $("#red, #green, #blue, #yellow").on('click', function() {
    sounds[this.id].play();
    $(this).fadeTo('fast', 0.33).fadeTo('fast', 1);
    playerArray.push(this);
    if (playerArray.length === stepArray.length) {
      $("#red, #green, #blue, #yellow").off(); //removes all the handlers from the colored fields
      is_same();
      label:
        if (!is_same()) {
          if (strict === true) {
            alert("Strict mode....start over again");
            newGame();
            break label;
          }
          alert("Wrong!");
          playAndAnimate();
        } else {
          updateStep(step + 1);
          playAndAnimate();
        }
    }
  });
  if (step === 20) alert("Great Memory You Win!");
}

$(document).ready(function() {
  $("#steps").html(step);
  $("#reset").click(function() {
    $("#red, #green, #blue, #yellow").off();
    updateStep(0); //return to the first step of the game
    playAndAnimate();
  });
  $("#start").click(newGame);
  $("#strict").click(function() {
    strict = !strict;
    var mode = strict === true ? "Strict" : "Regular";
    $("#red, #green, #blue, #yellow").off();
    $("#strict").html(mode);
    alert("Playing " + mode);
    newGame();
  });
});

            
          
!
999px
Loading ..................

Console