Guest User

Untitled

a guest
Apr 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. // ========
  2. // ##Traffic Lights
  3. // ========
  4.  
  5. function doTrafficLights() {
  6. const activeLight = getActiveLight();
  7. if(activeLight === 'red'){
  8. return turnRed();
  9. }
  10. if(activeLight === 'yellow'){
  11. return turnYellow();
  12. }
  13. if(activeLight === 'green'){
  14. return turnGreen();
  15. }
  16. console.log(activeLight);
  17.  
  18. }
  19.  
  20. // this function randomly returns red, yellow, or green
  21. // and is called by doTrafficLights.
  22. // don't modify it!
  23. function getActiveLight() {
  24. return (['red', 'green', 'yellow'])[Math.floor(Math.random() * 3)];
  25. }
  26.  
  27. /* From here down, you are not expected to
  28. understand.... for now :)
  29.  
  30.  
  31. Nothing to see here!
  32.  
  33. */
  34.  
  35. function turnOffLights() {
  36. $('.traffic-light').removeClass('yellow-on red-on green-on');
  37. }
  38.  
  39. function turnGreen() {
  40. turnOffLights();
  41. $('.green-light').addClass('green-on');
  42. }
  43.  
  44. function turnYellow() {
  45. turnOffLights();
  46. $('.yellow-light').addClass('yellow-on');
  47. }
  48.  
  49. function turnRed() {
  50. turnOffLights();
  51. $('.red-light').addClass('red-on');
  52. }
  53.  
  54.  
  55. function handleClicks() {
  56. $('.js-control-lights').click(function() {
  57. doTrafficLights();
  58. });
  59. }
  60.  
  61. $(function() {
  62. turnOffLights();
  63. handleClicks();
  64. });
  65.  
  66. // ========
  67. // ##Error Alert
  68. // ========
  69.  
  70. function main() {
  71. try {
  72. doAllTheThings();
  73. }
  74. catch (err) {
  75. console.error(err);
  76. reportError(err);
  77. }
  78. }
  79.  
  80. function doAllTheThings() {
  81. throw {
  82. message: "Everything's ruined",
  83. name: "FatalException",
  84. toString: function() {
  85. return `${this.name}: ${this.message}`;
  86. }
  87. }
  88. }
  89.  
  90. function reportError(e) {
  91. $('.js-error-report').text(`Uh oh, something went wrong! Here's what we know: ${e.message}`);
  92. }
  93.  
  94. $(main);
  95.  
  96. // =======
  97. // ##
  98. // =======
Add Comment
Please, Sign In to add comment