OK so let's start with the first one here.
Print all the numbers between negative 10 and 19.
So before we actually start writing code we need to make our files.
So I have a simple Tim file
and I need to create a script to connect.
So let's save our script.
Let's call it loops.
Yes save that and then I'll add an alert because I always want to make sure they're connected.
Go back to wild study DML and then just link to that script which was loops that.
Yes.
So now if we go and open that up we should get our nice alert.
Great.
So let's tackle the first one.
Print all numbers between negative 10 and 19.
So we're going to start with a WHILE loop.
We're going to start with the variable var let's say counter starts at negative 10 then we'll just have
a wild loop where we're checking while counter is less than 20.
Cancel that log counter and then we'll add 1 to counter just like that.
So we started negative 10.
We printed out we add 1 while Kountry is less than 20.
We could also refactor this to be while it's less than or equal to 19.
Either one works in a do this one just for variety sake so save.
And let's go at a con. that log up top just to make it clear we're printing all numbers between negative
10 and 19 save and run this.
So will refresh the page.
Don't see anything because it's all on the console and you can see we get all numbers between negative
10 and 19.
Make sure it includes 19 negative 10.
Great.
So the next challenge is to print out all even numbers between 10 and 40.
So I'm just going to copy this and modify it just a little bit.
So printing all even numbers between 10 and 40 save.
So we'll start with our counter just equal to 10 while counter is less than 41 or less than or equal
to 40.
Let's do it this way.
We're going to print it out only if it's even.
So there's a few ways to do this.
The first one is that we always print it out but we count by twos.
So this means that we start at 10.
Then we add then we print 10 out and we add two and then we are at 12.
So then we print out 12 and then we add two.
So that's one way of doing it just to make sure that works.
Let's refresh open the console and you can see printing all evens between 10 and 40.
And that works fine.
So there's another way that we could have written this.
And I'll just copy this to show you.
We could write it this way.
So we loop through for every number.
So we don't count by twos and then we're going to have an if statement in here to check if a number
is even.
And that looks like if counter mod 2 is zero.
So that's how you tell for numbers even if it's evenly divisible by two then we'll print it out and
then we'll just add one.
So this way is shorter and it's a little more efficient because this loop is only going to run half
as many times as this one because we're counting by twos.
So we never run this loop for the odd numbers versus this one down here.
If we start with 10 if counter 2 is zero it is constant at like 10.
We add 1.
So now we do it for 11.
So this code is evaluated is 11 divisible by 2 and it's not.
So this isn't printed out but the code in the loop is still run every single time.
So there is one advantage of this set up here which is it will always print out the even numbers no
matter what number we start it versus this one.
If we started at 11 it's always going to just add to to that base number.
So we'll have 11 13 15 which aren't even but this one is actually checking if they are even.
So I'm going to comment that out for now and let's go with just the short version which for insert the
even numbers between 10 and 40 as we see here.
So next up we have print all odd numbers between 300 and 333.
So again it's very very similar to this.
I'm just going to copy all of this again and change this so printing all odd numbers between 300 and
333 and then we're going to start with counter at 300.
So this one won't work because we're starting at 300.
If we add two there will be 300 two and that will count by evens.
So I'm going to get rid of it and let's use the slightly longer way.
So to check if something is odd We want to make sure that it's not divisible by 2.
So that looks like if counter Montu is not equal to zero then we'll print out counter and the other
thing we need to change is this loop is going to start at 300 and we're checking if it's less than 40
.
So right now this code won't even run.
So we want to make this 333 So let's save and refresh and you can see we get all the odd numbers 3 or
1 all the way to 333.
So the last one here is to print all numbers divisible by five and three that are between 5 and 50.
So going to copy are set up one more time.
So this time I'm going to cancel that log printing all numbers divisible by
5 and 3 between 5 and 50.
So to do that we're going to start with our counter at 5 and it's going to go until 50.
And our logic here is not checking if it's not divisible by 2 which is what we have here.
What we want to do is check if it is divisible by 5 and divisible by 3.
So that's going to look like counter maade 5 equals zero and counter maade three equals zero and that's
it.
So both of these need to be true divisible by five evenly and 3 in order for us to printed out.
So that's all we need to do.
Let's go ahead and refresh our browser and you can see we get all numbers that are divisible both by
5 and 3 that are between 5 and 50 and there's only three of them.
All right so that's it for this while loops problem set.