So I'm going to go ahead and write the four solutions for these four loops.
So I'm going to make a new file save it.
I'll just call it for loops that HMO and I'll just copy my setup from the while loops.
So very similar.
The only difference is I'm going to change the title to be for loops problem set and then I'm going
to change the script to be for loops dot yes and then I'll make that file.
So make a new file save it for loops dot J us and instead of here as I always like to do
just put something so that we can tell that our loops are connected.
So now if we go ahead and open that up
tells us connected OK.
So the very first problem was DePrince
.
So the very first problem is to print all the numbers between negative 10 and 19.
So I'm going to copy the wild loop code that I have and move it over to four loops and I'm actually
going to make this split screen so that you can see both happening at the same time.
So I have my wild loop version on the left and then my FOR loop version on the right.
So what I need to do is print all the numbers between negative 10 and 19.
So for loop and then just do for I VAR I starts at negative 10 and we're going to keep running while
I is less than 20.
And then we're going to add one every time through the loop con. dog I and that's it.
So it's substantially shorter.
You don't have to make a separate variable.
We don't need a separate line to increment the variable.
We can do it all at once appear.
So let's just make sure that works back to the browser refresh.
Open up the console and we see negative 10 all the way to 19.
So next up printing all the even numbers between 10 and 40.
So there's a few ways we can do this.
We can start we need four.
Either way start i equal to 10 and we want to keep going.
Well I is less than or equal to 40.
So I'm just going to add some spaces.
Technically it doesn't matter.
I just like how it looks.
And then every time through we can either add two.
So I plus two equals two
and that's it.
Contador log I.
And this is the first way of doing it.
And just to demonstrate that back here refresh and we get all the even between 10 and 40.
But again the problem with this solution it's the same problem we had with the first while loop solution
.
If instead I wanted to print the evens between 11 and 40.
I'm going to have a problem.
And that's because I'm just adding to.
And that's because I'm just blindly adding to no matter what the starting value is.
So I'm getting the odd numbers this time.
So if I wanted to refactor it to match the other version where we actually check if a number is even
and needs to look like this I add one every time.
And then I have an if statement and I say if the number is even.
So if I had two equals zero then we'll come to that log.
I just like that.
And we shouldn't notice a difference very fresh still works just the same that this is the more foolproof
way where I can change this to be an odd number and it still doesn't matter.
Now it just prints the evens starting from 13 to 40.
All right.
The next one printing all the odd numbers between 300 and 333.
So we want another for loop.
We'll start i equal to 300.
We want to keep going.
Well I asked less than or equal to 3 3 3 and we'll just add one every time.
And then what we'll do just like we did up here instead of checking if something is even Well check
if it's odd.
So it's very similar.
If I my two is not equal to zero that means that it's odd it's not divisible by two then we'll cancel
that log and say that refresh and we get all the odd numbers starting at 3 or 1 up to 3 133.
So the very last one printing all the numbers divisible by five and three that are between five and
50.
So another for loop we start or I equal to five.
We keep going while it's less than or equal to 50 and we add one at the end of every loop.
Then what we're going to do is have an if statement check if I is divisible by five
then we're going to check if it also divisible by three I like that.
And then we cancel the log and that's it we're done.
So let's check it out.
Refresh and you can see we get 15 30 and 45.
So to recap here on the left I have the while loop solutions and on the right I have the for loop solutions
so I'll try and get them to match up as perfectly as possible
.
There we go.
And you can see the for loop solutions are considerably shorter.