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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css"></link>
<link href='https://fonts.googleapis.com/css?family=Orbitron' rel='stylesheet' type='text/css'>
<link href="https://fonts.googleapis.com/css?family=Josefin+Slab" rel="stylesheet">
</head>
<div id = "wrapper">
  <div class = "color" id = "green"></div>
  <div class = "color" id = "red"></div>
  <div class = "color" id = "yellow"></div>
  <div class = "color" id = "blue"></div>    
  <div id = "middle">
    <div id = "display">--</div>
    <i id ="power" class="fa fa-power-off" aria-hidden="true"></i>
    <button id="start">start/reset</button>
    <button id="strictMode">strict mode</button>
    <h1>SIMON</h1>
  </div>
</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Alfa+Slab+One|VT323|Oswald');

body {
  background-color: black;
}
#power {
  color: dimgrey;
  border-radius: 5px;
  cursor: pointer;
  font-size: 40px;
  border-style: none;
  position: absolute;
  top: 110px;
  left: 106px;
}
#display {
  letter-spacing: 1px;
  border-radius: 5px;
  font-family: "Orbitron";
  color: #b70c0c;
  font-size: 14px;
  line-height: 2.25;
  text-align: center;
  position: absolute;
  top: 50px;
  left: 50px;
  width: 150px;
  height: 30px;
  background-color: #380000;
  transition: 0.5s;
}
#start, #strictMode {
  border-radius: 3px;
  border-style: none;
  color: white;
  cursor: pointer;
  padding: 5px;
  background-color: grey;
  font-family: "Josefin Slab";
  font-size: 16px;
  position: absolute;
  transition: 0.2s;
}
h1 {
  color: white;
  font-size: 30px;
  font-family: "Josefin Slab";
  position: absolute;
  bottom: 20px;
  left: 72px;
}
#start {
  bottom: 105px;
  left: 15px;
}
#strictMode {
  bottom: 105px;
  right: 10px;
}
#power.on {
  color: orange;
}
button:focus {
  outline: none !important;
}
#middle {
  border-style: solid;
  border-width: 5px;
  border-color: grey;
  border-radius: 175px;
  height: 250px;
  width: 250px;
  background-color: #1a1a1a;
  position: absolute;
  margin-left: 125px;
  margin-top: 125px;
}
#wrapper {
  background-color: black;
  position: relative;
  border-radius: 255px;
  height: 520px;
  width: 520px;
  margin: 100px auto;
}
#red, #blue, #green, #yellow {
  border-style: solid;
  border-width: 3px;
  cursor: pointer;
  width: 250px;
  height: 250px;
  float: left;
}
#red {
  border-top-right-radius: 250px;
  background-color: #c61515;
  opacity: 0.5;
}
#blue {
  border-bottom-right-radius: 250px;
  background-color: #0047cc;
  opacity: 0.5;
}
#green {
  border-top-left-radius: 250px;
  background-color: #167a00;
  opacity: 0.5;
}
#yellow {
  border-bottom-left-radius: 250px;
  background-color: #e0e01f;
  opacity: 0.5;
}
#red.active {
  opacity: 1;
}
#blue.active {
  opacity: 1;
}
#green.active {
  opacity: 1;
}
#yellow.active {
  opacity: 1;
}
#start.active, #strictMode.active {
  background-color: green;
}

              
            
!

JS

              
                $(document).ready(function() {
  const red = "#red";
  const blue = "#blue";
  const green = "#green";
  const yellow = "#yellow";
  const redAudio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
  const blueAudio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
  const greenAudio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
  const yellowAudio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
  const colors = [red, blue, green, yellow];
  var seq = [];
  var player = [];
  var playerTurn = false;
  var power = false;
  var lost = false;
  var strict = false;
  var turn = 1;
  var index = 0;
  var timeout;
    
  //power on/off
  $("#power").on("click", function() {
    power = (!power) ? true : false;
    if (power) {
      $(this).addClass("on");
      $("#display").html("PRESS START");
    } else {
      newGame();
      $(this).removeClass("on");
      $("#display").html("--");
      $("#start").removeClass("active");
      $("#strictMode").removeClass("active");
    }
  });

  //turn on strict mode
  $("#strictMode").click(function() {
    if (power) {
      strict = (!strict) ? true : false;
      if (strict)
        $(this).addClass("active");
      else
        $(this).removeClass("active");
    }
  });

  //start game
  $("#start").on("click", function() {
    if (power) {
      $("#display").html("PLAY!");
      $("#start").addClass("active");
      newGame();
      setTimeout(computerTurn, 1500);
      clearTimeout(computerTurn);
    }
  });
  
    //player input/audio
  $(".color").mousedown(function() {
    if (power && playerTurn) {
      var $button = $(this);
      playSounds($button);
      $($button).addClass("active");
      $(this).mouseup(function() {
        $(".color").removeClass("active");
      });
    }
  });

  //reset for new game
  const newGame = () => {
    clearTimeout(timeout);
    $(".color").removeClass("active");
    $(".color").off("click");
    seq = [];
    player = [];
    turn = 1;
    playerTurn = false;
    lost = false;
    generateSeq();
  }

  //run computer sequence
  const runSeq = () => {
    if (!power)
      return;
    $("#display").html(index + 1);
    var $next = $(seq[index]);
    $next.addClass("active");
    playSounds($next);
    timeout = setTimeout(function() {
      $next.removeClass("active");
      index++;
      if (index >= turn) {
        clearTimeout(timeout);
        playerTurn = true;
        player = [];
        playerTry();
      } else {
        setTimeout(runSeq, 200);
        clearTimeout(runSeq);
      }
    }, 800);
  }

  //switch to AI turn
  const computerTurn = () => {
    index = 0;
    runSeq();
  }

  //players turn
  const playerTry = () => {
    if (playerTurn) {
      $(".color").on("click", function() {
        var tag = "#";
        var picked = tag.concat($(this).attr("id"));
        player.push(picked);
        $("#display").html(player.length);
        if (strict) {
          if (!(picked == seq[player.length - 1])) {
            $(".color").off("click");
            $("#display").html("YOU LOSE");
            $("#start").removeClass("active");
            lost = true;
            newGame();
          }
          //change turns if player succeeded
          else if (player.length >= turn && !lost) {
            $(".color").off("click");
            playerTurn = false;
            turn++;
            setTimeout(computerTurn, 1000);
            clearTimeout(computerTurn);
          } else if (player.length == 20) {
            $("#display").html("YOU WIN!");
            $("#start").removeClass("active");
            newGame();
          }
        } else if (!strict) {
          if (!(picked == seq[player.length - 1])) {
            player.pop();
            $("#display").html("TRY AGAIN");
            $(".color").off("click");
            playerTurn = false;
            setTimeout(computerTurn, 2000);
            clearTimeout(computerTurn);
          }
          //change turns if player succeeded
          else if (player.length >= turn && !lost) {
            $(".color").off("click");
            playerTurn = false;
            turn++;
            setTimeout(computerTurn, 1000);
            clearTimeout(computerTurn);
          } else if (player.length == 20) {
            $("#display").html("YOU WIN!");
            $("#start").removeClass("active");
            newGame();
          }
        }
      });
    }
  }

  //generates random sequence of 20
  const generateSeq = () => {
    for (var i = 0; i <= 20; i++) {
      random = Math.floor(Math.random() * colors.length);
      seq.push(colors[random]);
    }
  }

  //sound controller
  const playSounds = x => {
    if (x.attr("id") == "red")  {
      redAudio.currentTime  = 0;
      redAudio.play();
    }      
    if (x.attr("id") == "blue") {
      blueAudio.currentTime  = 0;
      blueAudio.play();
    }
      
    if (x.attr("id") == "green")
      {
        blueAudio.currentTime = 0;
        greenAudio.play();
      }
      
    if (x.attr("id") == "yellow") {
      yellowAudio.currentTime = 0;
       yellowAudio.play();
    }
     
  }
});
              
            
!
999px

Console