JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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='container-fluid'>
<div id='title'>Repeat after me</div>
<div id='options'><p id='start'>Start</p>Strict? <input type="checkbox" id='strict'><p id='reset'>Reset</p></div>
<div id='simon'>
<div class='button dark' id='red'></div>
<div class='button dark' id='green'></div>
<div class='button dark' id='blue'></div>
<div class='button dark' id='yellow'></div>
</div>
<div class='hidden' id='wrong'>Wrong :(</div>
<div class='hidden' id='strictLoss'>Strict mode on, resetting!</div>
<div class='hidden' id='yay'>You did it! You won!! Now resetting...</div>
<div id='count'>Count<p id='totalCount'>0</p></div>
</div>
* {
height: 100%;
padding: 0;
margin: 0;
font-family: 'Amita', cursive;
}
#title {
height: auto;
text-align: center;
font-size: 2.5em;
}
#start, #strict {
height: auto;
}
#simon {
height: 250px;
width: 250px;
position: absolute;
top: 100px;
left: calc(50% - 125px);
background-color: #6d6d6d;
text-align: center;
border-radius: 5px;
}
.button {
height: 100px;
width: 100px;
border-radius: 10px;
display: inline-block;
}
#red {
margin-top: 20px;
margin-right: 6px;
background-color: red;
}
#green {
background-color: #3ff45a;
}
#blue {
background-color: #4286f4;
margin-right: 6px;
}
#yellow {
background-color: yellow;
}
.dark {
opacity: 0.2;
}
#options {
position: fixed;
top: 100px;
left: 100px;
font-size: 2em;
}
#count {
position: fixed;
top: 100px;
right: 100px;
font-size: 2em;
}
#wrong {
height: auto;
width: 175px;
position: fixed;
top: 150px;
left: calc(50% - 87.5px);
z-index: 100;
color: #ff0000;
font-size: 3em;
}
#strictLoss {
height: auto;
width: 540px;
position: fixed;
top: 220px;
left: calc(50% - 270px);
z-index: 100;
color: #ff0000;
font-size: 3em;
}
#yay {
height: auto;
width: 500px;
position: fixed;
top: 130px;
left: calc(50% - 250px);
z-index: 100;
color: #00b220;
font-size: 3em;
background-color: #303030;
text-align: center;
border-radius: 10px;
}
.hidden {
visibility: hidden;
}
var pattern = [];
var player = [];
var possibilities = ['red', 'green', 'blue', 'yellow'];
var addOne;
var i = 0;
var redAudio = document.createElement('audio');
redAudio.setAttribute('src', 'https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
var blueAudio = document.createElement('audio');
blueAudio.setAttribute('src', 'https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
var greenAudio = document.createElement('audio');
greenAudio.setAttribute('src', 'https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
var yellowAudio = document.createElement('audio');
yellowAudio.setAttribute('src', 'https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
$('#start').on('click', function() {
addCount();
$('#start').off('click');
})
$('#reset').on('click', function() {
$('.button').off('click');
player = [];
pattern = [];
$('#totalCount').html(pattern.length);
$('#start').on('click', function() {
addCount();
$('#start').off('click');
})
})
function addCount() {
addOne = Math.floor(Math.random() * 4);
pattern.push(possibilities[addOne]);
$('#totalCount').html(pattern.length);
playPattern();
}
function playPattern() {
setTimeout(function() {
$('#' + pattern[i]).removeClass('dark');
if (pattern[i] == 'red'){
redAudio.play();
} else if (pattern[i] == 'green'){
greenAudio.play();
} else if (pattern[i] == 'blue'){
blueAudio.play();
} else if (pattern[i] == 'yellow'){
yellowAudio.play();
}
setTimeout(button.bind(null, pattern[i]),400);
i++;
if (i < pattern.length) {
playPattern();
} else if (i == pattern.length) {
i = 0;
player = [];
playerTurn();
}
},700);
}
function button(color, delay) {
$('#' + color).addClass('dark');
}
function playerTurn () {
$('.button').on('click', function(){
player.push(this.id);
if (player.length < pattern.length &&
player[player.length-1] == pattern[player.length-1]) {
playButton(this.id);
} else if (player.length == pattern.length &&
player[player.length-1] == pattern[player.length-1] &&
pattern.length < 20) {
$('.button').off('click');
playButton(this.id);
setTimeout(addCount,500);
} else if (player.length == pattern.length &&
player[player.length-1] == pattern[player.length-1] &&
pattern.length == 20) {
$('#yay').removeClass('hidden');
$('.button').off('click');
setTimeout(function(){$('#yay').addClass('hidden');},2000);
setTimeout(strictLoss,2500);
} else if (player[player.length-1] != pattern[player.length-1]) {
$('#wrong').removeClass('hidden');
$('.button').off('click');
setTimeout(function(){$('#wrong').addClass('hidden');},1000);
if (document.getElementById('strict').checked) {
$('#strictLoss').removeClass('hidden');
setTimeout(function(){$('#strictLoss').addClass('hidden');},1000);
setTimeout(strictLoss,1500);
} else {
setTimeout(playPattern,1500);
}
}
})
}
function strictLoss() {
player = [];
pattern = [];
$('#totalCount').html(pattern.length);
addCount();
}
function playButton(box) {
$('#' + box).removeClass('dark');
if (box == 'red'){
redAudio.play();
} else if (box == 'green'){
greenAudio.play();
} else if (box == 'blue'){
blueAudio.play();
} else if (box == 'yellow'){
yellowAudio.play();
}
setTimeout(button.bind(null, box),300);
}
Also see: Tab Triggers