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.

            
              <body ontouchstart="">
<div id="shell">
  <div id="green" class="press-buttons"></div>
  <div id="red" class="press-buttons"></div>
  <div id="yellow" class="press-buttons"></div>
  <div id="blue" class="press-buttons"></div>
  <div id="panel">
    <div id="logo" class="noselect">Simon</div>
    <div id="count-display" class="noselect">--</div>
    <div id="count-label" class="labels noselect">COUNT</div>
    <div id="start-button" class="buttons"></div>
    <div id="start-label" class="labels noselect">START</div>
    <div id="strict-lamp"></div>
    <div id="strict-button" class="buttons"></div>
    <div id="strict-label" class="labels noselect">STRICT</div>
    <div id="off-label" class="labels noselect">OFF</div>
    <div id="power-well">
      <div id="power-switch"></div>
    </div>
    <div id="on-label" class="labels noselect">ON</div>
  </div>
</div>
  <div id="instructions">
    <p>If you make a mistake in normal mode, the pattern will be repeated until you get it right.
    <p>If you make a mistake in strict mode, the game will start over from the beginning with a new pattern.
  </div>
</body>
            
          
!
            
              .noselect {
  /* add "noselect" class to items you want to prevent selection on */
  /* prevents text from being selected on buttons */
  -webkit-touch-callout: none;
  /* iOS Safari */
  -webkit-user-select: none;
  /* Chrome/Safari/Opera */
  -khtml-user-select: none;
  /* Konqueror */
  -moz-user-select: none;
  /* Firefox */
  -ms-user-select: none;
  /* Internet Explorer/Edge */
  user-select: none;
  /* Non-prefixed version, currently
                                  not supported by any browser */
}


body {
  background-color: #bbbb99;
}

#shell {
  width: 500px;
  height: 500px;
  background-color: black;
  border-radius: 50%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  align-content: center;
  margin: auto;
  position: relative;
  top: 10px;
  box-shadow: inset 5px 4px 15px 0px #333333, inset -5px 4px 20px 0px #333333, inset 5px -4px 20px 0px #333333;
}

.press-buttons {
  width: 44%;
  height: 44%;
  border: 10px solid black;
}

#panel {
  background: silver;
  background: linear-gradient(200deg, grey 5%, white 30%, grey 90%);
  height: 50%;
  width: 50%;
  position: absolute;
  top: 22.5%;
  left: 22.5%;
  border: 10px solid black;
  border-radius: 50%;
  font-size: 10px;
  box-shadow: inset 0px 0px 3px 2px gray;
}

#green {
  border-top-left-radius: 100%;
  background-color: #009900;
  box-shadow: inset 0px 0px 70px 0px #595959;
}

/*
#green:active {
  background: radial-gradient(#ccffcc, #77ff77, #33ff33, #00ff00);
}
*/

#red {
  border-top-right-radius: 100%;
  background-color: #aa0000;
  box-shadow: inset 0px 0px 70px 0px #595959;
}

/*
#red:active {
  background: radial-gradient(#ff8888, #ff5555, #ff2222, #ff0000);
}
*/

#yellow {
  border-bottom-left-radius: 100%;
  background-color: #bbbb00;
  box-shadow: inset 0px 0px 70px 0px #595959;
}

/*
#yellow:active {
  background: radial-gradient(#ffffcc, #ffff66, #ffff33, #ffff00);
}
*/

#blue {
  border-bottom-right-radius: 100%;
  background-color: #0000cc;
  box-shadow: inset 0px 0px 70px 0px #595959;
}

/*
#blue:active {
  background: radial-gradient(#aaaaff, #7777ff, #4444ff, #0000ff);
}
*/

#logo {
  height: 40px;
  margin: auto;
  text-align: center;
  font-size: 6em;
  font-weight: bold;
  position: absolute;
  top: 10%;
  left: 18%;
  cursor: default;
}

@font-face {
  font-family: digital;
  src: url(http://www.superconvert.com/codeplaypen/FCCcalculator/digital-monoitalic.ttf);
}

#count-display {
  font-family: digital;
  height: 22%;
  width: 32%;
  background-color: #1a0000;
  text-align: center;
  line-height: 110%;
  color: #4d0000;
  font-size: 5em;
  position: absolute;
  top: 43%;
  left: 10%;
  border-radius: 15px;
  box-shadow: inset 0px 0px 4px 1px black;
  cursor: default;
}

.buttons {
  height: 10%;
  width: 10%;
  border-radius: 50%;
  border: 1px solid #777777;
  box-shadow: 1px 1px 3px 1px #777777, inset -1px -1px 2px 1px #222222;
  position: absolute;
  cursor: pointer;
}

.buttons:active {
  box-shadow: 0.5px 0.5px 3px 1px #777777, inset -1px -1px 2px 1px #222222;
  border: 0;
}

#start-button {
  background-color: #ff0000;
  left: 52%;
  top: 50%;
}

#strict-button {
  background-color: #dddd00;
  left: 72%;
  top: 50%;
}

#strict-lamp {
  background-color: #770000;
  width: 5%;
  height: 5%;
  border-radius: 50%;
  position: absolute;
  top: 43%;
  left: 74.5%;
  box-shadow: inset 0 0 6px 0px, 1px 1px 1px 0px grey;
}

#power-well {
  background-color: #222222;
  width: 20%;
  height: 10%;
  position: absolute;
  left: 40%;
  top: 80%;
}

#power-switch {
  width: 45%;
  height: 85%;
  position: absolute;
  left: 4%;
  top: 10%;
  border-radius: 5%;
  background: -webkit-linear-gradient(left, #4444dd 80%, #0000aa 20%);
  background-size: 3px;
  cursor: pointer;
}

.labels {
  position: absolute;
  font-size: 1.2em;
  cursor: default;
}

#count-label {
  top: 68%;
  left: 18%;
}

#start-label {
  top: 68%;
  left: 50%;
}

#strict-label {
  top: 68%;
  left: 70%;
}

#off-label {
  top: 83%;
  left: 28%;
}

#on-label {
  top: 83%;
  left: 62%;
}

#instructions {
  width: 70%;
  max-width: 500px;
  margin: auto;
  position: relative;
  top: 50px;
  font-size: 120%;
}
            
          
!
            
              var isPowerOn = false;
var strictMode = false;
var sequenceStarted = false;
var pressCount = 0;
var count = 0;
var arrRandom = [];
var sequenceTimeout = [];
var enableButtonsTimeout;
var displayOnInterval;
var displayOffInterval;
var displayOnIntervalSet = false;
var displayOffIntervalSet = false;

function cancelFlashSequence() {
  // cancel remainder of flashes if mid-sequence
  for (var i = 0; i < count; i++) {
    clearTimeout(sequenceTimeout[i]);
  }
  clearTimeout(enableButtonsTimeout);
}

function togglePower() {
  if (!isPowerOn) {
    document.getElementById("power-switch").style.left = "50%"; // slide switch right
    document.getElementById("count-display").style.color = "#990000"; // turn display on
    isPowerOn = true;
    // need to load AND play audio files because iOS won't play them unless
    // played in the function the touch event calls. If you don't want to
    // actually play the sounds, immediately pause them to silence them
    audio1 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
    audio1.play(); audio1.pause();
    audio2 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
    audio2.play(); audio2.pause();
    audio3 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
    audio3.play(); audio3.pause();
    audio4 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
    audio4.play(); audio4.pause();
    audio5 = new Audio("http://www.soundjay.com/misc/fail-buzzer-01.wav");
    audio5.play(); audio5.pause();
  } else if (isPowerOn) {
    isPowerOn = false;
    disableButtons();
    // slide switch left and turn off strict-lamp and display
    // turn off strictMode, disableButtons
    document.getElementById("power-switch").style.left = "4%";
    document.getElementById("count-display").style.color = "#4d0000";
    document.getElementById("count-display").innerHTML = "--";
    document.getElementById("strict-lamp").style.backgroundColor = "#770000";
    strictMode = false;
    cancelFlashSequence();
  }
}

function toggleStrict() {
  if (isPowerOn) { // can only toggle if power is on
    if (!strictMode) {
      document.getElementById("strict-lamp").style.backgroundColor = "#ff0000";
      strictMode = true;
    } else if (strictMode) {
      document.getElementById("strict-lamp").style.backgroundColor = "#770000";
      strictMode = false;
    }
  } else if (!isPowerOn) { // do nothing if power is off
    return;
  }
}

function generateRndNum(min, max) {
  var rndNum = Math.floor(Math.random() * ((max - min) + 1) + min);
  return rndNum;
}

function generateArray() {
  // generate array of 20 random numbers between 1 and 4
  var arr = [];
  for (var i = 0; i < 20; i++) {
    rndNum = generateRndNum(0, 3);
    arr.push(rndNum);
  }
  return arr;
}

function flashGreenLight() {
  var el = document.getElementById("green");
  el.style.background = "radial-gradient(#ccffcc, #77ff77, #33ff33, #00ff00)";
  // Even though we created new Audio for each sound in togglePower(), we need to do so
  // here too because otherwise the file has to finish playing completely before
  // playing the same file a second time (such as a double-click)
  audio4 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
  audio4.play();
}

function flashRedLight() {
  var el = document.getElementById("red");
  el.style.background = "radial-gradient(#ff8888, #ff5555, #ff2222, #ff0000)";
  audio3 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
  audio3.play();
}

function flashYellowLight() {
  var el = document.getElementById("yellow");
  el.style.background = "radial-gradient(#ffffcc, #ffff66, #ffff33, #ffff00)";
  audio1 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
  audio1.play();
}

function flashBlueLight() {
  var el = document.getElementById("blue");
  el.style.background = "radial-gradient(#aaaaff, #7777ff, #4444ff, #0000ff)";
  audio2 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
  audio2.play();
}

function buttonLightOn(e) {
  if (e.target.className === "press-buttons") {
//    console.log("You pressed the " + e.target.id + " button");
    if (e.target.id === "green") {
      flashGreenLight();
    } else if (e.target.id === "red") {
      flashRedLight();
    } else if (e.target.id === "yellow") {
      flashYellowLight();
    } else if (e.target.id === "blue") {
      flashBlueLight();
    }
  }
}

function greenLightOff() {
  var el = document.getElementById("green");
  el.style.background = "#009900";
}

function redLightOff() {
  var el = document.getElementById("red");
  el.style.background = "#aa0000";
}

function yellowLightOff() {
  var el = document.getElementById("yellow");
  el.style.background = "#bbbb00";
}

function blueLightOff() {
  var el = document.getElementById("blue");
  el.style.background = "#0000cc";
}

function buttonLightOff(e) {
  if (e.target.className === "press-buttons") {
    if (e.target.id === "green") {
      greenLightOff();
    } else if (e.target.id === "red") {
      redLightOff();
    } else if (e.target.id === "yellow") {
      yellowLightOff();
    } else if (e.target.id === "blue") {
      blueLightOff();
    }
  }
}

function enableButtons() {
  document.getElementById("green").style.cursor = "pointer";
  document.getElementById("red").style.cursor = "pointer";
  document.getElementById("yellow").style.cursor = "pointer";
  document.getElementById("blue").style.cursor = "pointer";
  document.body.addEventListener("mousedown", pressButton);
  document.body.addEventListener("mouseup", releaseButton);
}

function disableButtons() {
  document.getElementById("green").style.cursor = "default";
  document.getElementById("red").style.cursor = "default";
  document.getElementById("yellow").style.cursor = "default";
  document.getElementById("blue").style.cursor = "default";
  document.body.removeEventListener("mousedown", pressButton);
  document.body.removeEventListener("mouseup", releaseButton);
}

function pressButton(e) {
  if (e.target.className == "press-buttons") {
    buttonLightOn(e);
  }
}

function releaseButton(e) {
  if (e.target.className == "press-buttons") {
    pressCount += 1;
    buttonLightOff(e);
    if (pressCount == count) {
      disableButtons();
    }
    checkInput(e);
  }
}

function updateDisplay(count) {
  var n = count + 1;
  n = n.toString();
//  console.log(n.length);
//  console.log("0" + n);
  if (n.length < 2) {
    n = ("0" + n);
  }
  document.getElementById("count-display").innerHTML = n; // display counter 0
}

function flashPanel() {
  displayOffInterval = setInterval(function() {
    document.getElementById("count-display").style.color = "#4d0000"; // off
  }, 1000);
  setTimeout(function() {
    displayOnInterval = setInterval(function() {
      document.getElementById("count-display").style.color = "#990000"; // on
    }, 1000);
  }, 500);
  displayOnIntervalSet = true;
  displayOffIntervalSet = true;
}

function checkInput(e) {
  var buttonPressed;
  if (e.target.className === "press-buttons") {
    if (e.target.id === "green") {
      buttonPressed = 0;
    } else if (e.target.id === "red") {
      buttonPressed = 1;
    } else if (e.target.id === "yellow") {
      buttonPressed = 2;
    } else if (e.target.id === "blue") {
      buttonPressed = 3;
    }
    if (arrRandom[pressCount - 1] == buttonPressed) {
      if (pressCount == count) {
//        console.log("You've entered the correct sequence!");
        if (count < arrRandom.length) {
          updateDisplay(count);
          playSequence(count);
        } else {
          flashPanel();
        }
      }
    } else {
      if (strictMode) {
        startGame();
      } else if (!strictMode) {
        count -= 1;
        playSequence();
      }
      audio1.pause();
      audio2.pause();
      audio3.pause();
      audio4.pause();
      audio5.play();
    }
  }
}

function playSequence(counter) {
//  console.log("playSeqence called"); // works ok
  count += 1;
//  console.log("count is now: " + count); // works ok
  counter = count;
  var pause = 1000; // initial pause at start of game
  for (var i = 0; i < counter; i++) {
    console.log("i = " + i);
    pause = pause + 600; // ms to pause before next flash
      if (arrRandom[i] === 0) {
        sequenceTimeout[i] = setTimeout(function() {
          flashGreenLight();
          setTimeout(greenLightOff, 300);
        }, pause);
      } else if (arrRandom[i] === 1) {
        sequenceTimeout[i] = setTimeout(function() {
          flashRedLight();
          setTimeout(redLightOff, 300);
        }, pause);
      } else if (arrRandom[i] === 2) {
        sequenceTimeout[i] = setTimeout(function() {
          flashYellowLight();
          setTimeout(yellowLightOff, 300);
        }, pause);
      } else if (arrRandom[i] === 3) {
        sequenceTimeout[i] = setTimeout(function() {
          flashBlueLight();
          setTimeout(blueLightOff, 300);
        }, pause);
      }
  }
  pressCount = 0;
  enableButtonsTimeout = setTimeout(enableButtons, pause + 600); // user can press buttons after seq is finished
}

function playGame() {
  arrRandom = generateArray();
  console.log(arrRandom);
  playSequence(count);
}

function init() {
  disableButtons();
  cancelFlashSequence();
  pressCount = 0;
  count = 0;
  updateDisplay(count);
  arrRandom = [];
  sequenceTimeout = [];
  enableButtonsTimeout;
  if (displayOffIntervalSet == true) {
    clearInterval(displayOffInterval);
    clearInterval(displayOnInterval);
    displayOffIntervalSet = false;
    displayOnIntervalSet = false;
    document.getElementById("count-display").style.color = "#990000"; // on
  }
}

function startGame() {
  if (isPowerOn) {
    init();
    playGame();
  } else { // do nothing if power is off
    return;
  }
}

document.getElementById("power-switch").addEventListener("click", togglePower);
document.getElementById("strict-button").addEventListener("click", toggleStrict);
document.getElementById("start-button").addEventListener("click", startGame);

            
          
!
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