Skip to content

Instantly share code, notes, and snippets.

@iancover
Last active January 18, 2022 11:55
Show Gist options
  • Save iancover/103d658b74772184f02eccd45d9647b9 to your computer and use it in GitHub Desktop.
Save iancover/103d658b74772184f02eccd45d9647b9 to your computer and use it in GitHub Desktop.
Thinkful jQuery exercises
// jQuery example
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);
}
$(main);
function doTrafficLights() {
turnOffLights();
var activeLight = getActiveLight();
if (activeLight === 'red') {
return turnRed();
}
if (activeLight === 'yellow') {
return turnYellow();
}
if (activeLight === 'green') {
return 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