Guest User

Untitled

a guest
Apr 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. function doTrafficLights() {
  2. const activeLight = getActiveLight();
  3. // your code will replace this call
  4. // to `console.log()`
  5.  
  6. if(activeLight === 'green') {
  7. turnGreen();
  8. } if (activeLight === 'yellow') {
  9. turnYellow();
  10. }
  11. if (activeLight === 'red') {
  12. turnRed();
  13. }
  14. }
  15.  
  16. // this function randomly returns red, yellow, or green
  17. // and is called by doTrafficLights.
  18. // don't modify it!
  19. function getActiveLight() {
  20. return (['red', 'green', 'yellow'])[Math.floor(Math.random() * 3)];
  21. }
  22.  
  23. /* From here down, you are not expected to
  24. understand.... for now :)
  25.  
  26.  
  27. Nothing to see here!
  28.  
  29. */
  30.  
  31. function turnOffLights() {
  32. $('.traffic-light').removeClass('yellow-on red-on green-on');
  33. }
  34.  
  35. function turnGreen() {
  36. turnOffLights();
  37. $('.green-light').addClass('green-on');
  38. }
  39.  
  40. function turnYellow() {
  41. turnOffLights();
  42. $('.yellow-light').addClass('yellow-on');
  43. }
  44.  
  45. function turnRed() {
  46. turnOffLights();
  47. $('.red-light').addClass('red-on');
  48. }
  49.  
  50.  
  51. function handleClicks() {
  52. $('.js-control-lights').click(function() {
  53. doTrafficLights();
  54. });
  55. }
  56.  
  57. $(function() {
  58. turnOffLights();
  59. handleClicks();
  60. });
Add Comment
Please, Sign In to add comment