Guest User

Untitled

a guest
Jun 25th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. //Traffic lights
  2. function doTrafficLights() {
  3. const activeLight = getActiveLight();
  4. if (activeLight === 'green') {
  5. return turnGreen();
  6. } else if (activeLight === 'yellow') {
  7. return turnYellow();
  8. } else {
  9. return turnRed();
  10. }
  11. }
  12.  
  13. function getActiveLight() {
  14. return (['red', 'green', 'yellow'])[Math.floor(Math.random() * 3)];
  15. }
  16.  
  17. function turnOffLights() {
  18. $('.traffic-light').removeClass('yellow-on red-on green-on');
  19. }
  20.  
  21. function turnGreen() {
  22. turnOffLights();
  23. $('.green-light').addClass('green-on');
  24. }
  25.  
  26. function turnYellow() {
  27. turnOffLights();
  28. $('.yellow-light').addClass('yellow-on');
  29. }
  30.  
  31. function turnRed() {
  32. turnOffLights();
  33. $('.red-light').addClass('red-on');
  34. }
  35.  
  36.  
  37. function handleClicks() {
  38. $('.js-control-lights').click(function() {
  39. doTrafficLights();
  40. });
  41. }
  42.  
  43. $(function() {
  44. turnOffLights();
  45. handleClicks();
  46. });
  47.  
  48. //Error alert
  49. function main() {
  50. try {
  51. doAllTheThings();
  52. } catch(err) {
  53. console.error(err);
  54. reportError(err);
  55. }
  56. }
  57.  
  58. function doAllTheThings() {
  59. throw {
  60. message: "Everything's ruined",
  61. name: "FatalException",
  62. toString: function() {
  63. return `${this.name}: ${this.message}`;
  64. }
  65. }
  66. }
  67.  
  68. function reportError(e) {
  69. $('.js-error-report').text(`Uh oh, something went wrong! Here's what we know: ${e.message}`);
  70. }
  71.  
  72. $(main);
Add Comment
Please, Sign In to add comment