Skip to content

Instantly share code, notes, and snippets.

@willwalker753
Last active February 20, 2020 23:39
Show Gist options
  • Save willwalker753/fb392f81eb9fff69beb10819e2453bdc to your computer and use it in GitHub Desktop.
Save willwalker753/fb392f81eb9fff69beb10819e2453bdc to your computer and use it in GitHub Desktop.
Traffic lights drill
function doTrafficLights() {
const activeLight = getActiveLight();
switch(activeLight) {
case 'red' : turnRed();
break;
case 'yellow': turnYellow();
break;
case 'green' : turnGreen();
break;
}
/*
if (activeLight === 'red') {
turnRed();
}
else if (activeLight === 'yellow') {
turnYellow();
}
else {
turnGreen();
}
*/
//console.log(activeLight);
}
// 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