123

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.

            
              <div class="circle">
  <div class="innerCircle">

    <div class="simon">Simon
    </div>
    <div class="buttons row">
      <div class="col-xs-4">
        <div class="number"> </div>
      </div>
      <div class="col-xs-4">
        <button id="startButton" class="button">Start</button>
      </div>
      <div class="col-xs-4">
        <div id="strict-toggle">
          <input id="strict-toggle2" type="checkbox" data-toggle="toggle" data-on="Strict" data-off="Strict Off" data-size="mini">
        </div>
      </div>
    </div>


    <div class="wrapper">
      <input id="power" class="power" type="checkbox" data-toggle="toggle">

    </div>


  </div>

  <div class="top">
    <div id="green" class="upperLeft colors">
    </div>
    <div id="yellow" class="bottomLeft colors">
    </div>
  </div>
  <div class="bottom">
    <div id="red" class="upperRight colors">
    </div>
    <div id="blue" class="bottomRight colors">
    </div>

  </div>
</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">You win!</h4>
      </div>
      <div class="modal-body">
        You passed level 20!
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Restart Game</button>
        
      </div>
    </div>
  </div>
</div>

            
          
!
            
              .circle {
    position: relative;
    top: 50px;
    margin: auto;
    text-align: center;
    border-radius: 50%;
    width: 500px;
    height: 500px;
    z-index: 0;
    display: flex;
}

.innerCircle {
    position: absolute;
    display: inline-block;
    left: 25%;
    top: 25%;
    background-color: white;
    border-radius: 50%;
    border: 10px solid black;
    width: 50%;
    height: 50%;
    margin: auto;
    z-index: 10;
    box-shadow: 0px 0px 100px black;
}

.upperLeft {
    border-top: 10px solid black;
    border-left: 10px solid black;
    border-right: 5px solid black;
    border-bottom: 5px solid black;
    width: 250px;
    height: 250px;
    border-top-left-radius: 100%;
    background: radial-gradient(lightgreen, green);
    box-shadow: 0px 0px 100px black;
}
.upperLeft.active:active {
    background: #bdf5bd!important;
}

.upperRight {
    border-top: 10px solid black;
    border-right: 10px solid black;
    border-left: 5px solid black;
    border-bottom: 5px solid black;
    width: 250px;
    height: 250px;
    border-top-right-radius: 100%;
    background: radial-gradient(red, firebrick);
    box-shadow: 0px 0px 100px black;
}

.upperRight.active:active{
    background:red;
    background:#ffb3b3!important;
}

.bottomLeft {
    border-bottom: 10px solid black;
    border-left: 10px solid black;
    border-top: 5px solid black;
    border-right: 5px solid black;
    width: 250px;
    height: 250px;
    border-bottom-left-radius: 100%;
    background: radial-gradient(LightGoldenRodYellow, yellow);
    background: radial-gradient(yellow, darkgoldenrod);
    box-shadow: 0px 0px 100px black;
}

.bottomLeft.active:active{
    background: yellow;
    background: LightGoldenRodYellow!important;
}

.bottomRight {
    border-bottom: 10px solid black;
    border-right: 10px solid black;
    border-top: 5px solid black;
    border-left: 5px solid black;
    width: 250px;
    height: 250px;
    border-bottom-right-radius: 100%;
    background: radial-gradient(royalblue, midnightblue);
    box-shadow: 0px 0px 100px black;
}

.bottomRight.active:active{
    background:royalblue;
    background: #91a8ee!important;
}

.mySpan {
    display: table-cell;
    vertical-align: middle;
    padding-right: 5px;
    padding-left: 5px;
}

.simon {
    position: relative;
    top: 10%;
    z-index: 11;
    font-size: 300%
}

.buttons {
    position: inherit;
    top:50%;
    transform: translateY(-50%);
    height: 85px;
    width:260px;
    /* border: 2px solid black; */
}

button {
    position: relative;
    vertical-align: middle;
}

.wrapper {
    position: relative;
    display: table;
    margin: auto;
    top: 48%;
}

.number {
    position: relative;
    top:50%;
    left:7px;
    transform: translateY(-50%);
    text-align: center;
    line-height: 40px;
    height:40px;
    width: auto;
    border: 2px solid black;
    z-index: 11;
    background:#3d3d29;
    border-radius:11px;
    color:crimson;
    font-family: 'Roboto', sans-serif;
    font-size: 27px
}
#strict-toggle {
  position:relative;
  top:50%;
  transform: translateY(-50%);
  left:-15px;
}


.strict{
    position: relative;
    top:45%;
    transform: translateY(-50%);
    left:0%;
    height: 30px;
    width:30px;
    border: 2px solid black;
    border-radius: 50%;
}
.strictInner{
    position: relative;
    top:50%;
    transform: translateY(-50%);
    margin: auto;
    height: 10px;
    width: 10px;
    border: 2px solid black;
    border-radius: 50%
}
.strictWords{
    position: relative;
    top:40%;
    transform:translateY(-50%);
    left:-14px;
    
}

.button {
    position: relative;
    top:50%;
    transform: translateY(-50%);
    font: bold 12px Arial, sans-serif;
    color: rgba(0, 0, 0, 0.8);
    text-shadow: 0 0.1em 0 rgba(255, 255, 255, 0.5);
    display: inline-block;
    outline: none;
    cursor: pointer;
    padding: 0.3em 1em 0.4em 1em;
    border-radius: 0.3em;
    border: 1px solid;
    border-color: #bbbbbb #a2a2a2 #888888;
    background-color: #eee;
    background-size: 100%;
    background-image: linear-gradient(#eeeeee, #d5d5d5);
    box-shadow: 0 0.1em 0.5em rgba(0, 0, 0, 0.1), 0 0.1em 0.2em rgba(0, 0, 0, 0.3), 0 -0.1em 0.07em rgba(0, 0, 0, 0.3) inset, 0 0.1em 0.07em rgba(255, 255, 255, 0.2) inset;
    transition: all 100ms;
}

.button:hover {
    box-shadow: 0 0.1em 0.5em rgba(0, 0, 0, 0.4), 0 0.1em 0.2em rgba(0, 0, 0, 0.4), 0 -0.1em 0.07em rgba(0, 0, 0, 0.3) inset, 0 0.1em 0.07em rgba(255, 255, 255, 0.2) inset;
}

.button:active {
    padding: 0.3em 1em 0.4em 1em;
    margin-top: 0.1em;
    border-color: #a2a2a2 #bbbbbb #eee;
    background-size: 100%;
    background-image: linear-gradient(#e1e1e1, #eeeeee);
    box-shadow: 0 0.1em 0.2em rgba(0, 0, 0, 0.1) inset, 0 0.1em 0.1em rgba(0, 0, 0, 0.2) inset, 0.05em 0 0.07em rgba(0, 0, 0, 0.2) inset, -0.05em 0 0.07em rgba(0, 0, 0, 0.2) inset;
    transition: all 10ms;
}

.col-xs-4 {
    height: 100%;
}
            
          
!
            
              var redSound = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
var blueSound = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
var yellowSound = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
var greenSound = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
var simonArr = []; //compare simon's array with player's array
var playerArr = []; //compare simon's array with player's array
var arrayLength;
var playerTurn = false;
var startButtonPressed = false;
var playerArrLength;

//function section ===================================
function addToColorsArr() {
  switch (Math.ceil(Math.random() * 4)) {
    case 1:
      simonArr.push("green");
      break;
    case 2:
      simonArr.push("red");
      break;
    case 3:
      simonArr.push("yellow");
      break;
    case 4:
      simonArr.push("blue");
      break;
  }
  arrayLength = simonArr.length;
  console.log("simonArr is: " + simonArr);
  console.log("simonArr[0] is: " + simonArr[0]);
  console.log("Array length is: " + arrayLength);
}

function startPressed() {
  //startbutton press code
  if ($("#power").is(":checked")) {
    //play simon function
    console.log("power is checked!")
    startButtonPressed = true;
    addToColorsArr();
    playSimon();

  } else {
    console.log("start pressed, power is NOT checked")
  }
}

function runThruColors(index) {
  if (index < simonArr.length) {
    //console.log("inside index < simonArr.length")
    if (simonArr[index] === "green") {
      greenSound.play();
      console.log("play green");
      $(".upperLeft").css("background", "#bdf5bd"); // turn color light
      setTimeout(function() {
        $(".upperLeft").css("background", "radial-gradient(lightgreen, green)");

        console.log("inside green setTimeout")
      }, 500); //turn color back to normal after 0.5 secs
    } else if (simonArr[index] === "red") {
      redSound.play();
      console.log("play red");
      $(".upperRight").css("background", "#ffb3b3"); // turn color light
      setTimeout(function() {
        $(".upperRight").css("background", "radial-gradient(red, firebrick)");
      }, 500); //turn color back to normal after 0.5 secs
    } else if (simonArr[index] === "yellow") {
      yellowSound.play();
      console.log("play yellow");
      $(".bottomLeft").css("background", "LightGoldenRodYellow"); // turn color light
      setTimeout(function() {
        $(".bottomLeft").css("background", "radial-gradient(yellow, darkgoldenrod");
      }, 500); //turn color back to normal after 0.5 secs
    } else if (simonArr[index] === "blue") {
      blueSound.play();
      console.log("play blue");
      $(".bottomRight").css("background", "#91a8ee"); // turn color light
      setTimeout(function() {
        $(".bottomRight").css("background", "radial-gradient(royalblue, midnightblue)");
      }, 500); //turn color back to normal after 0.5 secs
    }
    console.log("Just before calling the recursive part of the function!!!")
    $(".number").html(arrayLength);
    setTimeout(function() {
      runThruColors(index + 1);
    }, 800);
  }
  playerTurn = true;
}

function playSimon() {
  //write function to play the simonArr
  if (playerTurn) {
    console.log("playerTurn: " + playerTurn)
  } else {
    //I don't think a for loop will work, needs recursive function
    runThruColors(0);
  }
}

//function used after clicking the color and player's turn
function testMatchArrays() {
  //not actual length, but need number for index
  playerArrLength = playerArr.length - 1;
  //move matches and isn't last move yet
  if (playerArr[playerArrLength] === simonArr[playerArrLength] && playerArr.length < simonArr.length) {
    //do nothing, wait for next user click
    console.log("testMatchArrays: do nothing, just wait for next user click.");

  } else if (playerArr[playerArrLength] === simonArr[playerArrLength] && playerArr.length >= simonArr.length && playerArr.length <= 19) { //arrays same length and match
    //player turn is finised because playerArr and simonArr are equal length... add to simonArr and play simon
    playerTurn = false;
    playerArr = [];

    if ($("#power").is(":checked")) {
      //wait a little bit to start next simon sequence.
      setTimeout(addToColorsArr, 800);
      setTimeout(playSimon, 1200);
      //addToColorsArr();
      //playSimon();
    }
  } else if (playerArr[playerArrLength] !== simonArr[playerArrLength]) {
    //player messed up, pressed wrong color

    if ($("#strict-toggle2").is(":checked")) {
      console.log("messed up, strict on");
      playerArr = [];
      simonArr = [];
      playerTurn = false;
      $(".number").html("- -");

      //add in something to blink all colors at once and then start over
      setTimeout(addToColorsArr, 800);
      setTimeout(playSimon, 1200);
      //addToColorsArr();
      //playSimon();

    } else {
      //strict off, run through colors again
      playerArr = [];
      //simonArr = []; don't use strict off!!!
      playerTurn = false;
      console.log("messed up, strict off!!!");
      //addToColorsArr();  don't use strict off!!

      //add in setTimeout and blink all colors at once
      $(".number").html("- -");
      setTimeout(function() {
        $(".number").html(simonArr.length);
      }, 900);
      setTimeout(playSimon, 1000);
      //playSimon();
    }
  } else if (playerArr[playerArrLength] === simonArr[playerArrLength] && playerArr.length >= 20) { //won 20 moves!!!
    //winning move!!!
    console.log("You won!!!");
    $('#myModal').modal('show');
    playerArr = [];
    simonArr = [];
    startButtonPressed = false;
    playerTurn = false;
    $(".number").html("- -");

  }

}

$(document).ready(function() {

  //power button pressed
  $("#power").change(function() {
    if ($("#power").is(":checked")) {
      console.log("Power checked");
      $(".colors").toggleClass("active");
      $(".number").html("- -");
    } else {
      console.log("Power unchecked");
      $(".colors").toggleClass("active");
      simonArr = [];
      playerArr = [];
      playerTurn = false;
      startButtonPressed = false;
      $(".number").html("");
    }
  });

  //run game if start button is pressed
  $("#startButton").click(function() {
    if (!startButtonPressed) {
      startPressed();
    } else {
      console.log("Start button already pressed.")
    }
  });

  //clicked a color
  $(".colors").click(function() {
    //if player's turn then check move
    if (playerTurn) {
      if (this.id === "red") {
        redSound.play();
        playerArr.push(this.id);
        console.log("red!!")

      } else if (this.id === "blue") {
        blueSound.play();
        playerArr.push(this.id);
        console.log("blue!")
      } else if (this.id === "green") {
        greenSound.play();
        playerArr.push(this.id);
        console.log("green!!!");
      } else if (this.id === "yellow") {
        yellowSound.play();
        playerArr.push(this.id);
        console.log("yellow");
      }
      console.log(playerArr);

      //check if playerArr === simonArr after adding color to playerArr
      testMatchArrays();
    }
  });

}); //document ready function
            
          
!
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