Skip to content

Instantly share code, notes, and snippets.

@michaelablackham
Created April 11, 2017 01:21
Show Gist options
  • Save michaelablackham/9104725b9e4743cd32532a783c304619 to your computer and use it in GitHub Desktop.
Save michaelablackham/9104725b9e4743cd32532a783c304619 to your computer and use it in GitHub Desktop.
Logic Drills
function main() {
try {
doAllTheThings();
}
catch(e) {
console.error(e)
reportError(e);
}
}
function doAllTheThings() {
throw {
message: "Everything's ruined",
name: "FatalException",
toString: function(){return this.name + ": " + this.message;}
}
}
function reportError(e) {
$('.js-error-report').text("Uh oh, something went wrong! Here's what we know: " + e.message);
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
$(main);
function doTrafficLights() {
var activeLight = getActiveLight();
console.log(activeLight);
if(activeLight === 'red') {
turnRed();
}
else if(activeLight === 'yellow') {
turnYellow();
}
else {
turnGreen();
}
}
function turnOffLights() {
$('.traffic-light').removeClass('yellow-on red-on green-on');
}
function turnGreen() {
turnOffLights();
$('.green-light').addClass('green-on');
}
function turnYellow() {
turnOffLights();
$('.yellow-light').addClass('yellow-on');
}
function turnRed() {
turnOffLights();
$('.red-light').addClass('red-on');
}
function getActiveLight() {
return (['red', 'green', 'yellow'])[Math.floor(Math.random() * 3)];
}
function handleClicks() {
$('.js-control-lights').click(function() {
doTrafficLights();
});
}
$(handleClicks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment