Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="wrapper container-fluid">
    <!--<h1 class="deep-orange-text text-accent-4">SIMON</h1>-->
    <div id="main">
        <div id="green" class="green-normal user-click"></div>
        <div id="red" class="red-normal user-click"></div>
        <div id="inner">
            <div id="name" class="lines">
                <span>SIMON<i>®</i></span>
            </div>
            <div id="count" class="lines">
                <span>COUNT</span>
                <div id="numbers"></div>
            </div>
            <div id="controls" class="lines">
                <div class="btns" id="row-1">
                    <p>
                        <input type="checkbox" id="start" />
                        <label for="start">START</label>
                    </p>
                </div>
                <div class="btns" id="row-2">
                    <p>
                        <input type="checkbox" id="strict" />
                        <label for="strict">STRICT</label>
                    </p>
                </div>
            </div>

        </div>
        <div id="blue" class="blue-normal user-click"></div>
        <div id="yellow" class="yellow-normal user-click"></div>

    </div>

    <div class="push">
        <!-- this element for implementing sticky footer-->
    </div>
</div>


<!-- FOOTER -->
<footer class="footer">
    <span>&copy; 2017 Developed by Manish Giri for FreeCodeCamp</span>
</footer>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Mirza|Ravi+Prakash|Roboto');
@import url('https://fonts.googleapis.com/css?family=Cormorant+Garamond:700');
@import url('https://fonts.googleapis.com/css?family=Kaushan+Script');
@import url('https://fonts.googleapis.com/css?family=Kumar+One|Open+Sans');
@import url('https://fonts.googleapis.com/css?family=Merriweather');
@import url('https://fonts.googleapis.com/css?family=Open+Sans');
@import url('https://fonts.googleapis.com/css?family=Lemonada');

html, body {
    height: 100%;
    margin: 0;
}

body {
    background: darkgray url("https://dl.dropbox.com/s/aoavaqxq4o0d398/concrete_seamless.png?dl=0");
}

.wrapper {
    min-height: 100%;
    margin: 20px auto -50px auto;
    text-align: center;

}


/* the nested circles */
#main {
    display: inline-block;
    background-color: #444;
    width: 500px;
    height: 500px;
    border-radius: 50%;
    border: 2px solid #333;
    position: relative;
}

#inner {
    display: inline-block;
    background-color: #eeeeee;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    border: 20px solid #444;
    position: absolute;
    top: 30%;
    left: 30%;
    z-index: 999;
}

#green {
    display: inline-block;
    /*background-color: lightgreen;*/
    width: 220px;
    height: 220px;
    border-top-left-radius: 100%;
    position: absolute;
    left:20px;
    margin-top: 15px;
    margin-left: -1px;

}

/* color for #green */
.green-normal {
    background-color: #1b5e20;
    border: 2px solid #1b5e20;
}

#red {
    display: inline-block;
    /*background-color: #CD5C5C;*/
    width: 220px;
    height: 220px;
    /*border: 2px solid #CD5C5C;*/
    border-top-right-radius: 100%;
    position: absolute;
    right:20px;
    margin-top: 15px;
    margin-right: -5px;

}

/* color for #red */
.red-normal {
    background-color: #b71c1c;
    border: 2px solid #b71c1c ;
}


#blue {
    display: inline-block;
    /*background-color: lightblue;*/
    width: 220px;
    height: 220px;
    /*border: 2px solid lightblue;*/
    border-bottom-right-radius: 100%;
    position: absolute;
    right: 20px;
    top: 50%;
    margin-top: 5px;
    margin-right: -5px;
}

/* color for #blue */
.blue-normal {
    background-color: #0d47a1;
    border: 2px solid #0d47a1;
}

#yellow {
    display: inline-block;
    /*background-color: lightyellow;*/
    width: 220px;
    height: 220px;
    /*border: 2px solid lightyellow;*/
    border-bottom-left-radius: 100%;
    position: absolute;
    left:20px;
    top: 50%;
    margin-top: 5px;
    margin-left: -4px;
}

/* color for #yellow */
.yellow-normal {
    background-color: #f9a825;
    border: 2px solid #f9a825;
}

/* styles for inner controls */

.lines {
    /*font-family: 'Cormorant Garamond', serif;*/

    font-family: 'Merriweather', serif;
}

#name {
    margin: 20px auto 0px auto;

}

#name span {

    /*font-family: 'Kaushan Script', cursive;*/
    font-family: 'Kumar One', cursive;
    font-size: 24px;
    color: #3e2723;
}

#name span i {
    font-size: 18px;
    font-style: normal;
    position: relative;
    top: -5px;
}

#count {
    font-size: 17px;
}

#count span {
    margin-left: -25px;
    position: relative;
    top: 1px;
    margin-right: 10px;
}

#numbers {
    display: inline-block;
    width: 45px;
    height: 30px;
    background-color: rgb(50,5,12);
    border: 2px solid black;
    margin-right: -25px;
    border-radius: 5px;
    color: #d32f2f;
    padding-top: 1px;
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
}

/* third row in inner circle */

#controls {
    margin-top: -6px;
}

#row-2 {
    margin-top: -10px;
    margin-left: 5px;
}

#row-1 p label {
	color: #004d40;
}


#row-2 p label {
	color: #e65100;
}

#row-1 {
    margin-top: 10px;
}


/* colors that will be applied to the div on button press "lighter" */
.green-lighter {
	background-color: #4caf50;
	border: 2px solid #4caf50;  
}

.red-lighter {
	background-color: #f44336;
	border: 2px solid #f44336;
}

.blue-lighter {
	background-color: #2196f3;
	border: 2px solid #2196f3;
}

.yellow-lighter {
	background-color: #ffeb3b;
	border: 2px solid #ffeb3b; 
}


/* sticky footer */
.footer, .push {
    height: 50px;
}

/* footer */

footer {
    background: linear-gradient(to right, black, #444);
    padding: 10px;
    text-align: center;
}
footer span{
    font-family: 'Lemonada', cursive;
    color: floralwhite;
    font-size: 11px;
}
              
            
!

JS

              
                /**
 * Created by manishgiri on 12/29/16.
 */
/**
 * Created by manishgiri on 12/28/16.
 */
/**
 * Created by manishgiri on 12/26/16.
 */
$(document).ready(function () {

    //reference to global variables
    var greenSound = '';
    var redSound = '';
    var blueSound = '';
    var yellowSound = '';

    var $numbers = $("#numbers");

    //array of colors
    var colors = ["green", "red", "blue", "yellow"];

    var computerSequence = [];
    var userSequence = [];


    //-----flag variables-----
    var sequenceMatch = false;
    var iterCount = 1;
    let isStrictChecked = false;

    //test notification
    //notifyUser();

    //for displaying count in box
    var turnCount = 0;
    $numbers.html(turnCount);


    //load sounds
    loadSounds();


    $('#start').on('change', function() {

        if ($(this).is(':checked')) {
            console.log('Start is checked');
            computerTurn(true);
            userTurn();
        }
        else {
            reset();
        }
        //console.log($(this).is(":checked"));
    });
    //console.log("outside");

    $("#strict").on('change', function () {
       if($(this).is(":checked")) {
           console.log(`Strict checked`);
           isStrictChecked = true;

       }
       else {
           console.log(`Strict not checked`);
           isStrictChecked = false;
       }
    });



    //------------------------------------------------------
    function computerTurn(lastSeq = false) {

        console.log("Inside computer turn function - line 1");

        //test - play last sequence

        if(lastSeq) {
            //if default parameter is not true, then add new button to sequence
            turnCount++;
            nextButton();
        }

        $numbers.html(turnCount);
        //call nextButton() to start the sequence
        //nextButton();


        console.log("Current computer sequence = ");
        console.log(computerSequence);

        //feature - decrease time between button presses as game progresses
        let speedValue = 1000;

        //when turnCount is between 1 and 5, speedValue = 1000
        //when turnCount is between 5 and 10, speedValue = 700
        //when turnCount is between 10 and 15, speedValue = 500
        //when turnCount is between 15 and 20, speedValue = 300

        if(turnCount >= 0 && turnCount < 5) {
            speedValue = 1000;
        }

        else if(turnCount >= 5 && turnCount < 10) {
            speedValue = 700;
        }

        else if(turnCount >= 10 && turnCount < 15) {
            speedValue = 500;
        }

        else if(turnCount >= 15 && turnCount <= 20) {
            speedValue = 300;
        }



        //generate through the sequence
        computerSequence.forEach(function (color, index) {
            (function (index) {
                setTimeout(function () {
                    switch (color) {
                        case "blue": blueButtonPress();
                            break;
                        case "red": redButtonPress();
                            break;
                        case "green": greenButtonPress();
                            break;
                        case "yellow": yellowButtonPress();
                            break;
                        default: console.log("Incorrect value");
                    }
                    //console.log("Waiting");
                }, speedValue * index);
            })(index);



        });

    }



    //------------------------------------------------------
    function userTurn() {
        console.log("Inside user turn function - line 1");
        userSequence = [];
        let incorrectSequence = false;
        let userColorNum = 0;

        //capture the button clicked
        $(".user-click").click(function () {

            userColorNum++;

            sequenceMatch = false;
            //userSequence = [];
            //fetch ID of current button pressed
            let currColor = this.id;
            console.log(`User clicked on ${currColor}`);
            userButtonPress(currColor);
            incorrectSequence = false;

            // //show user sequence
            // console.log("Current user sequence: ");
            // console.log(userSequence);

            //-------------------------------------------
            //add it to user sequence

            userSequence.push(currColor);
            //show user sequence
            console.log("Current user sequence: ");
            console.log(userSequence);

            //test lodash
            if(_.isEqual(userSequence, computerSequence)) {
                if(turnCount === 20) {
                    notifyGameWin();
                    reset();
                    return;
                }
                console.log("Inside lodash");
                console.log("Current computer sequence = ");
                console.log(computerSequence);
                console.log("Current user sequence: ");
                console.log(userSequence);
                userSequence = [];
                sequenceMatch = true;

            }
            else {

                if(userSequence.length <= computerSequence.length) {
                    console.log("Inside else");
                    if(userSequence[userColorNum-1] !== computerSequence[userColorNum-1]) {
                        console.log(`Wrong color pressed---`);
                        console.log(`Comp color was ${computerSequence[userColorNum]} but user color is ${userSequence[userColorNum]} `);
                        userSequence.pop();
                        incorrectSequence = true;
                        userColorNum = 0;

                    }

                }



                // userSequence.pop();
                // incorrectSequence = true;
            }
            //userColorNum++;


            //if sequence is incorrect, repeat last sequence
            if(incorrectSequence) {
                if(isStrictChecked) {
                    incorrectSequence = false;
                    computerSequence = [];
                    userSequence = [];
                    setTimeout(() => {
                        //begins a new sequence, so set count to 0
                        //notifyIncorrectButton();
                        turnCount = 0;
                        $numbers.html(turnCount);
                        computerTurn(true);
                    }, 1000);
                }
                else {
                    incorrectSequence = false;
                    userSequence = [];
                    setTimeout(() => {
                        computerTurn();
                    }, 1000);
                }

            }

            //if last sequence was correct, begin next sequence with a new button included

            if(sequenceMatch) {
                console.log("Inside sequence match if");
                userSequence = [];
                //turnCount++;
                sequenceMatch = false;

                userColorNum = 0;

                //userSequence = [];
                setTimeout(()=>{
                    iterCount++;
                    //update score
                    //$numbers.html(turnCount);
                    computerTurn(true);
                }, 1500);
            }

        })


    }



    //-------------------------------------------------------

    //--------UTILITIES--------------

    //-------------------------------------------------------


    function nextButton() {
        //generate a random number to pick next button to be pressed
        let randomNumber = Math.floor(Math.random() * colors.length);
        console.log(`Random number is ${randomNumber}`);
        let pickedColor = colors[randomNumber];
        //console.log(`Picked color is ${pickedColor}`);
        //add picked color to sequence array
        computerSequence.push(pickedColor);
        return pickedColor;
    }

    //function for computer to simulate button press without actual clicks on buttons
    function userButtonPress(color) {
        console.log(`User's button to be pressed - ${color}`);
        switch(color) {
            case "green": greenButtonPress(); break;
            case "red": redButtonPress(); break;
            case "blue": blueButtonPress(); break;
            case "yellow": yellowButtonPress(); break;
            default: console.log("Incorrect button press");
        }

    }

    //---currently unused-----
    function notifyIncorrectButton() {
        swal({
            title: "Uh-Oh!",
            text: "<p style='color: red;'>You messed up.</p>",
            html: true,
            timer: 6000,
            showConfirmButton: true,
            type: "warning",
            confirmButtonColor: '#004d40'
        });
    }
    
    function notifyGameWin() {
        swal("Good job!", "You won the game!", "success");
    }




    function reset() {
        console.log("Reset called");
        computerSequence = [];
        console.log(`Computer sequence = `);
        console.log(computerSequence);
        //console.log("");
        userSequence = [];
        console.log(`User sequence = `);
        console.log(userSequence);
        turnCount = 0;
        $numbers.html(turnCount);
    }




    //------------------------------------------------------
    //function to play sound and change color for each button press
    function greenButtonPress() {

        $("#green").removeClass("green-normal");
        $("#green").addClass("green-lighter");
        greenSound.play();
        setTimeout(() => {
            $("#green").removeClass("green-lighter");
            $("#green").addClass("green-normal");
        }, 300);
    }


    //red button
    function redButtonPress() {
        $("#red").removeClass("red-normal");
        $("#red").addClass("red-lighter");
        redSound.play();
        setTimeout(() => {
            $("#red").removeClass("red-lighter");
            $("#red").addClass("red-normal");
        }, 300);
    }

    //blue button
    function blueButtonPress() {
        //blue button sound

        $("#blue").removeClass("blue-normal");
        $("#blue").addClass("blue-lighter");
        blueSound.play();
        setTimeout(() => {
            $("#blue").removeClass("blue-lighter");
            $("#blue").addClass("blue-normal");
        }, 300);
    }

    //red button
    function yellowButtonPress() {
        $("#yellow").removeClass("yellow-normal");
        $("#yellow").addClass("yellow-lighter");
        yellowSound.play();
        setTimeout(() => {
            $("#yellow").removeClass("yellow-lighter");
            $("#yellow").addClass("yellow-normal");
        }, 300);
    }

    //------------------------------------------------------


    //function to initalize sounds
    function loadSounds() {
        greenSound = new Howl({
            src: ["https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"]
        });

        redSound = new Howl({
            src: ["https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"]
        });

        blueSound = new Howl({
            src: ["https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"]
        });

        yellowSound = new Howl({
            src: ["https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"],
            volume: 1.0,
        });

    }


});
              
            
!
999px

Console