OK welcome back.
So as I mentioned at the end of the last video on iterating through an array with four loops and four
inches this is going to be a very quick exercise.
There's only one here and all you need to do is evaluate this code.
So we have two arrays and we're doing a for each.
So take a moment try and walk through this mentally or remember what we talked about how foreach works
how it's different than a for loop what array iteration is.
All of that and take a moment to think about what this prints out and then we'll go over it.
All right.
So puzzle video don't cheat.
OK great.
So let's take a moment first to appreciate how impressive that animation was.
Here let me show it to you again and I can even change colors.
And there's a few more animations Tim's like you're not really that impressed.
So let's just talk about the solution I guess.
So to arrays numbers which is an array of numbers going from one to 10 and colors are all colors array
red orange yellow and green for strings.
Then we're running numbers dot for each.
And I tried to confuse you a little bit where I threw a few wrenches in here where we have the variable
name color which is not a good name for this for each because we're not dealing with colors.
We're running for each on the numbers array.
So we're running foreach on this array and it takes this whole function right there.
And it applies it to every single item in this array.
So in this case 10 different numbers.
And again recalling that temporary variable name the placeholder color but it's going to be referring
to numbers in our case but it doesn't actually matter as far as JavaScript concerned.
This is just like any other function argument that we declare that we write it can be named whatever
we want as long as it follows the basic rules of javascript where it's a valid name for a variable.
And then I threw a little Maggiolo in here so if color mod 3 is equal to zero.
So what that means is that the number needs to be evenly divisible by three.
If that's the case then we print out color which is actually a number.
So loop through all of them.
This will be run but only on a few of them will this constant out log be executed.
So the numbers that are divisible by 3 are 3 6 and 9.
So we would expect to see 3 6 and 9.
And just to walk you through that first few numbers.
First time through this code is called with 10:01 as color.
So color is equal to 1 if color mod 3 is zero.
That's false.
So we're done then.
For each does the same thing with two.
And it passes two into this function as color.
So color mod 3 that's also not equal to zero.
So we don't don't like it.
And then next this is the last and I'll do three is passed in to this function.
So colors three three my three is zero.
So we find they print out colored.
So let's copy this up and evaluate the whole thing and we get 3 6 and 9.
Awesome.
So that's all we need to do here.
Quick exercise.
I made it a little bit tricky.
What would be a good practice for you.
Another good exercise is to go through and just rewrite this code that will only print out 3 6 and 9
.
Only the numbers that are divisible by three.
So not just always 3 6 and 9 but if we had 50 items in this array it should always print out the ones
divisible by three but use a for loop rather than for each.
So rewrite or translate this code into a for loop.
Great.
I'll see in the next video where we return to our To-Do list and we implement a re iteration