Created
August 18, 2018 07:49
-
-
Save punkrocker178/823bd84922a958dd8f31d99d23d18b2e to your computer and use it in GitHub Desktop.
Thinkful Unit 2 Lesson 3 Logic drills
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*-----------------------------------Drill 1-------------------------------*/ | |
function doTrafficLights() { | |
var activeLight = getActiveLight(); | |
// your code will replace this call | |
// to `console.log()` | |
if(activeLight==="red"){ | |
turnRed(); | |
} | |
else if(activeLight==="green"){ | |
turnGreen(); | |
} | |
else if(activeLight==="yellow"){ | |
turnYellow(); | |
} | |
} | |
/* 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 getActiveLight() { | |
return (['red', 'green', 'yellow'])[Math.floor(Math.random() * 3)]; | |
} | |
function handleClicks() { | |
$('.js-control-lights').click(function() { | |
doTrafficLights(); | |
}); | |
} | |
$(handleClicks); | |
/*-----------------------------------Drill 2-------------------------------*/ | |
function main() { | |
try{ | |
doAllTheThings(); | |
}catch(err){ | |
console.error(err); | |
reportError(err); | |
} | |
} | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment