So in this video we're going to do a code along and it's going to illustrate one other way to use a
while loop.
So so far we've been using a while loop where we use numbers and we have a range.
So we print numbers from 10 to 50 and that is a set range.
So there's another way to use a while loop where we don't actually know exactly how many times it will
run it's not pre-determined.
So this annoy o matic that we're going to make it.
It's a fancy term for an annoying Web site that constantly asks you Are We There Yet.
Until you enter the word yes or yeah.
Anything else to ask you Are we there yet again.
So let me show you I'll refresh the page.
Are we there yet.
Let's say no.
Are we there yet.
Not yet.
Are we there yet.
And now let's say Yes.
And it tells us yes we finally made it.
So that's what we're going to make to start.
So to get going.
We need to have a file with our HMO.
And I just called mine and noid HMO and then we're going to add in our scripts tag
and then we'll need to make the file.
So let's call this annoyed.
Yes let's make our new file a noise.
J.S. in the same directory and I always like to add my alert connected just to make sure.
Now let's open up our version and we won't see anything on the page but it should say connected.
All right.
So the first thing that we want to do is just ask a user are we there yet.
So we'll make it a variable called the answer equal to prompt.
Are we there yet.
Save that.
So then the next step is to check did the user say yes.
So we could use an if statement like this if answer equals equals yes then we'll do an alert.
Yay we made it and that's great and we could have an else where we then ask again and you don't have
to do this part.
But I'm just illustrating to you without a while loop we could do this but it will only run one time
so it will ask are we there yet.
And if I enter no then it will run the else.
Var answer equals are we there yet again.
And then nothing else happens.
So I'll show you that refresh are we there yet.
No.
Are we there yet.
No and that's it.
So rather than using just a simple conditional we're going to use a while loop but it's going to use
a lot of the same code that we have here.
So we just write while answer is not equal to yes.
So while the user has not said yes we're going to prompt them again.
And that's all there is to it.
So while the answer is not equal to the word yes let's ask them again.
Are we there yet.
And then finally at the very end we can just have an alert that says hey we made it and this won't run
Intel this loop is over.
And so that means that the user has to have typed in yes in order to get this far.
So let's try it out go back to the browser are we there yet.
No.
Nope.
No.
Anything we want.
And then as soon as we type the word yes it tells us yea we made.
So I wanted to do this to show you that you can use a while loop in another way.
So this is not a pre-determined loop that runs 10 times or five times.
It can vary and it's all up to user input.
So there was one more requirement which is that we could also type the word Yeah.
Why E.H. instead of yes.
And either one would work.
So that's pretty easy to implement.
All we need to do is add in a another part of this conditional where we want to add an and well answer
is not equal to yes.
And answer is not equal to.
Yeah.
Then we'll ask again.
And that's it.
So refresh types of thing it keeps asking then I could enter.
Yeah.
And it says We made it and I can also do that entering Yes.
So there's a version 2 here that's just a little bit more advanced where you don't have to enter the
exact word.
Yes or yeah only you could enter any phrase as long as has.
Yes or yeah in it.
So I'll show you.
It could be something like.
Yes we are which currently doesn't work in our other version because it's checking to see if the entire
string is yes or yeah.
So what we want to do is check if the string contains Yes if part of it matches yes but not all of it
necessarily.
So to do that there's a method we can use called index of.
So I'll show you an example in the con..
If I make a string called Hello world and I use index of.
So as Tiare index of in in parentheses I give it a string like w.
It's going to give me the number where w occurs which is 6 0 1 2 3 4 5 6.
But I can also use it on a full word like world and it tells me that world occurs starting at index
6 and if I try and do something that doesn't exist.
So in our case let's try Yes that's not in Hello world.
It gives me negative 1 so I can use that to my advantage.
So I want to check is yes anywhere in the input string anywhere an answer.
So to do that and it go back down here and for our version to I'm just going to keep our initial code
and commented out and then all right version 2.
And put this down here.
So this is going to stay the same.
This is going to be different.
So we're going to want something like answer that index of.
Yes.
And we want to see is it equal to negative 1.
So that means that the word yes does not exist inside of our answer.
So let's try that out.
Refresh are we there yet.
No.
Are we there yet.
No.
Are we there yet.
Yes I think so.
And it says yeah we made it.
Try it again and let's try.
We are there.
Yes.
And it tells us we made it.
OK.
So that's version 2 and that's all we needed to do there.
So that's all there is to version 2.
I just want to show that you can use index of to make your input checking a little bit more flexible