Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. // Traffic Lights Exercise
  12.  
  13. function doTrafficLights() {
  14. var activeLight = getActiveLight();
  15. if (activeLight == "green") {
  16. return turnGreen();
  17. } else if (activeLight == "red"){
  18. return turnRed();
  19. } else if (activeLight == "yellow"){
  20. return turnYellow();
  21. }
  22. }
  23.  
  24. function turnOffLights() {
  25. $('.traffic-light').removeClass('yellow-on red-on green-on');
  26. }
  27.  
  28. function turnGreen() {
  29. turnOffLights();
  30. $('.green-light').addClass('green-on');
  31. }
  32.  
  33. function turnYellow() {
  34. turnOffLights();
  35. $('.yellow-light').addClass('yellow-on');
  36. }
  37.  
  38. function turnRed() {
  39. turnOffLights();
  40.  
  41. // Error Alert Exercise
  42.  
  43. function main() {
  44. try {
  45. doAllTheThings();
  46. }
  47. catch(e) {
  48. console.error(e);
  49. reportError(e);
  50. }
  51. }
  52.  
  53. function doAllTheThings() {
  54. throw {
  55. message: "Everything's ruined",
  56. name: "FatalException",
  57. toString: function(){return this.name + ": " + this.message;}
  58. }
  59. }
  60.  
  61. function reportError(e) {
  62. $('.js-error-report').text("Uh oh, something went wrong! Here's what we know: " + e.message);
  63. }
  64.  
  65. $(main);
  66. </script>
  67.  
  68.  
  69.  
  70. <script id="jsbin-source-javascript" type="text/javascript">// Traffic Lights Exercise
  71.  
  72. function doTrafficLights() {
  73. var activeLight = getActiveLight();
  74. if (activeLight == "green") {
  75. return turnGreen();
  76. } else if (activeLight == "red"){
  77. return turnRed();
  78. } else if (activeLight == "yellow"){
  79. return turnYellow();
  80. }
  81. }
  82.  
  83. function turnOffLights() {
  84. $('.traffic-light').removeClass('yellow-on red-on green-on');
  85. }
  86.  
  87. function turnGreen() {
  88. turnOffLights();
  89. $('.green-light').addClass('green-on');
  90. }
  91.  
  92. function turnYellow() {
  93. turnOffLights();
  94. $('.yellow-light').addClass('yellow-on');
  95. }
  96.  
  97. function turnRed() {
  98. turnOffLights();
  99.  
  100. // Error Alert Exercise
  101.  
  102. function main() {
  103. try {
  104. doAllTheThings();
  105. }
  106. catch(e) {
  107. console.error(e);
  108. reportError(e);
  109. }
  110. }
  111.  
  112. function doAllTheThings() {
  113. throw {
  114. message: "Everything's ruined",
  115. name: "FatalException",
  116. toString: function(){return this.name + ": " + this.message;}
  117. }
  118. }
  119.  
  120. function reportError(e) {
  121. $('.js-error-report').text("Uh oh, something went wrong! Here's what we know: " + e.message);
  122. }
  123.  
  124. $(main);</script></body>
  125. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement