Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Traffic lights
- function doTrafficLights() {
- const activeLight = getActiveLight();
- if (activeLight === 'green') {
- return turnGreen();
- } else if (activeLight === 'yellow') {
- return turnYellow();
- } else {
- return turnRed();
- }
- }
- function getActiveLight() {
- return (['red', 'green', 'yellow'])[Math.floor(Math.random() * 3)];
- }
- 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();
- });
- //Error alert
- 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}`);
- }
- $(main);
Add Comment
Please, Sign In to add comment