Created
          March 11, 2018 17:22 
        
      - 
      
 - 
        
Save liseferguson/28882d4599d66c02595bd52f1ab754e9 to your computer and use it in GitHub Desktop.  
    Traffic Light Function drill
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function doTrafficLights() { | |
| const activeLight = getActiveLight(); | |
| if (activeLight === 'red') { | |
| turnRed(); | |
| } | |
| else if (activeLight === 'green') { | |
| turnGreen(); | |
| } | |
| else { | |
| turnYellow(); | |
| } | |
| } | |
| // this function randomly returns red, yellow, or green | |
| // and is called by doTrafficLights. | |
| // don't modify it! | |
| function getActiveLight() { | |
| return (['red', 'green', 'yellow'])[Math.floor(Math.random() * 3)]; | |
| } | |
| /* From here down, you are not expected to | |
| understand.... for now :) | |
| Nothing to see here! | |
| */ | |
| function turnOffLights() { | |
| $('.traffic-light').removeClass('yellow-on red-on green-on'); | |
| } | |
| function turnGreen() { | |
| turnOffLights(); | |
| $('.green-light').addClass('green-on'); | |
| } | |
| function turnYellow() { | |
| turnOffLights(); | |
| $('.yellow-light').addClass('yellow-on'); | |
| } | |
| function turnRed() { | |
| turnOffLights(); | |
| $('.red-light').addClass('red-on'); | |
| } | |
| function handleClicks() { | |
| $('.js-control-lights').click(function() { | |
| doTrafficLights(); | |
| }); | |
| } | |
| $(function() { | |
| turnOffLights(); | |
| handleClicks(); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
https://repl.it/@liseferguson/Traffic-lights-drill