Unit 2-Lesson3-Project 3: Logic Drills
function doTrafficLights() {
var activeLight = getActiveLight();
console.log(activeLight);
if(activeLight==='green'){
turnGreen();
}
else if(activeLight==='yellow'){
turnYellow();
}
else if(activeLight==='red'){
turnRed();
}
}
/* 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);
function main() {
try {
doAllTheThings();
}
catch(e) {
console.error(e);//shows up in console//
reportError(e);//shows up in output
}
}
function doAllTheThings() {
throw {
message: "Everything's ruined",
name: "FatalException",
toString: function(){return this.name + ": " + this.message;}
}
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
function reportError(e) {
$('.js-error-report').text("Uh oh, something went wrong! Here's what we know: " + e.message);
}
$(main);