U4Ch1L9: Functions Explore / Investigate
Purpose: Students will use Functions to remove repeated code from a program.
Video: Intro to Functions
Video: Defining & Calling Functions.
Activity: U4Ch1L9_Level1: Predicting How Many Times a Function Will Be Called.
- View the Program, then predict how many times the Chorus will be printed in the Console Log.
- Run the Program
Activity: U4Ch1L9_Level2: Moving Call Functions to Appropriate Spots.
- Read the program. Look for the comments that ask you to ‘Add code to sing chorus.”
- Find the two ‘Call Functions’ {singChorus ();} and slide them below these comments.
Activity: U4Ch1L9_Level3: Declaring & Calling Functions.
- View the Program, then predict what will occur.
- Answer the Following:
- On what line is the Function being Declared?
- On what lines is the Function being Called?
Activity: U4Ch1L9_Level4: Calling Functions: Update Screen.
- View the Program
- On what lines is the updateScreen function called?
Activity: U4Ch1L9_Level 5: Update Screen.
- Run the Program – How does “updateScreen” make the program easier to write and understand?
Do It:
- Change the program to make the game end when the score is 15 or greater.
- Change the program to make the game end when the score is -10 or less.
- Change the program to make it display the word " clicks" after the count as you play the game. For example, it should say "5 clicks" or "10 clicks" instead of just 5 or 10
Answer.
Activity: U4Ch1L9_Level 6: Declaring & Calling Functions.
- This is the Lemon Squeeze app you worked on earlier in the unit. It has repeated blocks of code that should be replaced by a Function.
- Look inside the event handler for the "lemon" and "lime" to find the repeated code.
- Replace the repeated blocks of code with a single function called updateScreen() that you declare once but call twice.
Answer.