Advertisement
Guest User

Untitled

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