Skip to content

Instantly share code, notes, and snippets.

@ecarlson1201
Created August 24, 2018 23:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecarlson1201/f559ed65e8477ceda87a5fdc1aeacd7e to your computer and use it in GitHub Desktop.
Save ecarlson1201/f559ed65e8477ceda87a5fdc1aeacd7e to your computer and use it in GitHub Desktop.
Control and Flow Drill
function main() {
try {
doAllTheThings()
} catch(e){
reportError(e)
console.dir(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}`);
}
$(main);
function doTrafficLights() {
const activeLight = getActiveLight();
if (activeLight === 'red') {
turnRed();
};
if (activeLight === 'yellow') {
turnYellow();
};
if (activeLight === 'green') {
turnGreen();
};
}
// this function randomly returns red, yellow, or green
// and is called by doTrafficLights.
// don't modify it!
function getActiveLight() {
return (['red', 'green', 'yellow'])[Math.floor(Math.random() * 3)];
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
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 handleClicks() {
$('.js-control-lights').click(function () {
doTrafficLights();
});
}
$(function () {
turnOffLights();
handleClicks();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment