This is one of the projects from freecodecamp.It's a game of Simon(a memory game). I would like a review on my code. var generatedPattern = []; var playerPattern = []; var patternLength = 1; var redRef = 1; var greenRef = 2; var blueRef = 3; var yellowRef = 4; function generateNextPatternValue() { if (patternLength == 20) { gameReset(); } else { var next = getRandomNum(1, 4); if (next === generatedPattern[generatedPattern.length - 1] && next == 4) next--; else if (next === generatedPattern[generatedPattern.length - 1]) next++; generatedPattern.push(next); animateGeneratedPattern(); } } function getRandomNum(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function animateRed(lightup) { if (lightup) { var audio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"); audio.play(); } $("#1").toggleClass("animateRed"); } function animateGreen(lightup) { if (lightup) { var audio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"); audio.play(); } $("#2").toggleClass("animateGreen"); } function animateBlue(lightup) { if (lightup) { var audio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"); audio.play(); } $("#3").toggleClass("animateBlue"); } function animateYellow(lightup) { if (lightup) { var audio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"); audio.play(); } $("#4").toggleClass("animateYellow"); } function animateGeneratedPattern() { var i = 0; function animateNextPattern(lightup) { if (!generatedPattern || generatedPattern.length === i) { return; } switch (generatedPattern[i]) { case 1: animateRed(lightup); break; case 2: animateGreen(lightup); break; case 3: animateBlue(lightup); break; case 4: animateYellow(lightup); break; } if (lightup) { // Long delay before turning light off setTimeout(function() { animateNextPattern(false); }, 1000); } else { // Small delay before turning on next light i++; setTimeout(function() { animateNextPattern(true); }, 100); } } animateNextPattern(true); } function gameReset() { if (patternLength == 20) alert("You Have Won"); document.getElementById("start").disabled = false; playerPattern = []; generatedPattern = []; patternLength = 1; updateLength(); restoreColors(); } function restoreColors() { $("#1").removeClass("animateRed"); $("#2").removeClass("animateGreen"); $("#3").removeClass("animateBlue"); $("#4").removeClass("animateYellow"); } function updateLength() { $("#length").empty(); var txtNode = document.createTextNode(patternLength); document.getElementById("length").appendChild(txtNode); } function animateScreen(turnOn) { if (turnOn) { var audio = new Audio("http://www.sounds.beachware.com/2illionzayp3may/illwavul/CLNKBEEP.mp3"); audio.play(); } $("body").toggleClass("wrongSeries"); } function wrongChoice() { animateScreen(true); setTimeout(function() { animateScreen(false); }, 100); } function unbind() { $("#1").unbind("click"); $("#2").unbind("click"); $("#3").unbind("click"); $("#4").unbind("click"); } function bind() { $("#1").bind("click", function() { if (document.getElementById("start").disabled) { playerPattern.push(1); if (checkSeries()) { animateRed(true); setTimeout(function() { animateRed(false); }, 500); } } }); $("#2").bind("click", function() { if (document.getElementById("start").disabled) { playerPattern.push(2); if (checkSeries()) { animateGreen(true); setTimeout(function() { animateGreen(false); }, 500); } } }); $("#3").bind("click", function() { if (document.getElementById("start").disabled) { playerPattern.push(3); if (checkSeries()) { animateBlue(true); setTimeout(function() { animateBlue(false); }, 500); } } }); $("#4").bind("click", function() { if (document.getElementById("start").disabled) { playerPattern.push(4); if (checkSeries()) { animateYellow(true); setTimeout(function() { animateYellow(false); }, 500); } } }); } function checkSeries() { if (playerPattern[playerPattern.length - 1] !== generatedPattern[playerPattern.length - 1]) { if (document.getElementById("strict").checked) { wrongChoice(); gameReset(); document.getElementById("start").disabled = true; setTimeout(generateNextPatternValue, 1500); } else { unbind(); wrongChoice(); playerPattern = []; setTimeout(animateGeneratedPattern, 1500); setTimeout(bind, 1500 * patternLength); } return false; } else if (playerPattern.length == patternLength) { unbind(); patternLength++; updateLength(); playerPattern = []; setTimeout(generateNextPatternValue, 1000); setTimeout(bind, 1500 * patternLength); return true; } return true; } window.addEventListener("load", updateLength); window.addEventListener("load", bind); $("#start").on("click", function() { document.getElementById("start").disabled = true; generateNextPatternValue(); }); $("#reset").on("click", gameReset);