For Loops
Loops are used in order to repeat the same instruction multiple times.
This is efficient and can help us save time, particularly when we lots of the same task to complete. We will be learning 2 out of the 3 methods for loops. The first method is a for loop, which allows us to repeat a command for a certain length. TASK: Create your first loop. 1.) Create a new folder called loops and create a web page with a button that calls a function called count. 2.) Copy and paste this code for (let i = 1; i < 5; i++) { alert(i) } 3.) Try it out and see what happens. Challenges: 1.) Try to count to 5 by adding an "=" 2.) Try changing the number to get it to count to 10 3.) Try to get the count to start from 5 4.) Try to get it to count from 10 down to 1 |
Times Table Display
Task: Create a times table code using a for loop.
1.) Create 2 variables: - timesNo - noCalulations 2.) Make the value of these whatever you want 3.) Create a for loop like this: for (let i = 1; i < 5; i++) { alert(i) } 4.) using the timesNo and the noCalculations, try to figure out how to get the alert to repeat (4x1 is 4, 4x2 is 8 etc...) If you figure it out do not help your friends!!! Extra for experts: Ask the user for what number they want to see the times table for and for how many times. |
Check out a demo here:
Hint: Using variables to control loops rather then numbers is known as using a constant, or derived value. This is excellence level coding.
|
Brief: Sum of even numbers
Task: Write a program that calculates the sum of all even numbers from 1 to 20 using a for loop. Display the final sum in an alert.
Arrays Introduction
An array is a variable.
An array is a variable that can store a whole heap of values. For example I could create an array called firstNames It could store all the names: Simon, Harry, Mary, Amy, Steve It would look like this in javaScript: firstNames=["Simon", "Harry", "Mary", "Amy", "Steve"]; This works similar to post boxes or pigeon holes. Each entry is given a number Simon's number is 0, Mary's Number is 2 If I wanted to alert Stevens number I would write: alert(firstNames[4]); Task: Read from the Array 1.) Create an array called lastNames to store these last names in this order: Sutherland, Tairea, Jorgensen, Bell, Tyler, Herring, Revill 2.) Create a series of alerts that calls the names in this order: Herring, Bell, Tairea, Sutherland, Jorgensen, Revill Bell, Herring, Jorgensen, Revill, Sutherland, Tairea, Tyler Hint Arrays can be created to look like this also firstNames[ "Simon", "Peter", "Matthew", ]; |
Extra for Experts:
Add your last name onto the array by using this code lastNames.push("YourLastName"); Display it last in the series of alerts. |
Combining Arrays and For Loops
For Loops and Arrays are the friends.
In fact they are best buddies. We can cycle through an array using the i variable. Think about it, a for loop that starts at 0 and ends at 4 can be used to go through an array with 5 entries Task: Create an array of fruit and make your program alert all of them. 1.) Create an array with 5 different fruit in it. Call it fruits. 2.) Enter this command and see what happens: alert(fruits.length); 3.) Using the fruits.length and i as your token, create a for loop that goes through the array and alerts each fruit. Extra for experts: Ensure that a banana is one of the fruits. Use an if statement that makes sure that "banana" is not alerted. |
Hint: Using the .length command in a for loop is known as a derived value and is important for an excellence grade.
This means that you can add any number of entries in and the for loop will still go through all of them, this is called flexibility. |
Brief: Pirate Crew Roll Call
Task: Ye be captaining a mighty pirate ship with a crew of scallywags! Write a program that has an array of pirate names, make one of them "Barnacle Mark the Bumbler". Use a for loop to iterate over the array and print each pirate's name along with a greeting, such as "Ahoy, [pirate name]!".
When it gets to "Barnacle Mark the Bumbler" instead of greeting him it should tell him to walk the plank.
When it gets to "Barnacle Mark the Bumbler" instead of greeting him it should tell him to walk the plank.
Brief: Farewell, my friend!
Task: Create the following array: let languages = ["Ka kite anō!", "À bientôt!", "Mata ne!"];
Create a for loop and use an if/else statement to ask the user if they want a farewell in Māori, French, or Japanese, then give them the appropriate farewell in that language in an alert.
Create a for loop and use an if/else statement to ask the user if they want a farewell in Māori, French, or Japanese, then give them the appropriate farewell in that language in an alert.
Code Commenting
Why is it important?
Unsurprisingly, in the corporate world you are normally not programming by yourself.
A game like Helldivers 2 has 100s or even 1000s of people working on it, a large portion of this group is made up of programmers who have to read over each others code. With this number of people, code has to be well written with appropriate documentation (like comments).
Code comments help other people to understand what the code is doing, especially in cases where it might not be obvious or unusual.
Task: Put some code comments through your for loop code (from the previous few activities) that explain what is happening on each line.
Unsurprisingly, in the corporate world you are normally not programming by yourself.
A game like Helldivers 2 has 100s or even 1000s of people working on it, a large portion of this group is made up of programmers who have to read over each others code. With this number of people, code has to be well written with appropriate documentation (like comments).
Code comments help other people to understand what the code is doing, especially in cases where it might not be obvious or unusual.
Task: Put some code comments through your for loop code (from the previous few activities) that explain what is happening on each line.
Checkpoint - Capitals of the World Quiz
Task: Capitals of the world Quiz
1.) Create 2 arrays, one with countries the other with the related capital of those countries.
2.) Use a for loop to cycle through the countries, ask the user "What is the capital of country [country here]"
3.) Check their answer with the capitals array using an if statement and inform them of whether they are right or wrong.
1.) Create 2 arrays, one with countries the other with the related capital of those countries.
2.) Use a for loop to cycle through the countries, ask the user "What is the capital of country [country here]"
3.) Check their answer with the capitals array using an if statement and inform them of whether they are right or wrong.