Day 4: Loops
Lesson 1: For
Here's an array:
1let family = ["Ben", "Emily", "Saj", "Vahid"];
We want to loop through that array and print the names out. We can do that with a for
loop. Conceptually a for
loop is:
1tasks before the loop 2 3for each number starting at 0 until the last number { 4do this thing 5} 6 7tasks after the loop
You can see how do this thing
is on it's own line, it's a self contained task that will be run for every iteration of the loop. The control of the loop is managed by for each number starting at 0 until the last number
and the tasks after the loop
won't run until every iteration of the loop is done.
Now let's look at the JavaScript for this:
1for (let loopCounter = 0; loopCounter < 5; loopCounter++) { 2 console.log(loopCounter); 3}
for () { }
tells the JavaScript engine that we are about to declare afor
loop. The contents of the( )
are the conditions for the loop and the code between the{ }
are what will be executed for each iteration of the loop.let loopCounter = 0;
this seeds ourfor
loop. We could call it anything (historicallyi
orindex
are used but calling itloopCounter
is clearer in my book) and we could start it at any value but0
makes the most sense as eventually we want to loop over that array.loopCounter < 5;
this is the run condition of thefor
loop. So long as loopCounter is less than 5 the code between the{ }
will be executed.loopCounter++
This is the change condition of the loop. Each time the code between the{ }
is executed. In this case we have used a shorthand forloopCounter = loopCounter + 1
, so each time the loop runsloopCounter
is increased by 1. We can use whichever calculation we want here, but since we want to look at each item in the array, increasing by 1 is perfect.console.log(loopCounter);
is between the for loops{ }
and so this is executed on each iteration of the loop.loopCounter
is accessible inside the{ }
and so we can see how it changes each loop.
Let's see it in action
Great! You should see 0, 1, 2, 3, 4 printed out one after another.
Notice how it starts at 0 (because that's what we seeded loopCounter
with) but ends at 4? Well it ends at 4 because the run condition loopCounter < 5;
is checked before the code is run so as soon as loopCounter
is not less than 5
it will stop the loop and move on to the next block of code.
So, how do we use this to print out our array?
The key bit here is using the ever increasing loopCounter
to read the contents of the array at each location by doing family[loopCounter]
.
Challenges
In the playground below I want you to see what happens when you:
- Start
loopCounter
at 2 - Start
loopCounter
at 10 - Set the loop to stop at 25
- Increase
loopCounter
by 2 instead of 1
If things become unresponsive it's because the playground got stuck in an infinite loop, so don't delete the new line I've added if (loopCounter === 500) break;
this is a crude infinite loop protector.
You are still quite likely to hit an infinite loop issue due to the way that the playground works, just refresh your page or, if that doesn't work, wait for your browser to realise it's probably in an infinite loop and it will show up a refresh message for you.