Going in the loop
Loops is a global name for a lot of different types of operations in programming that enable us to trigger the same block of code more than one time.
One of the biggest dangers with working with loops is creating an endless loop. An endless loop is a loop that has a bug, an ill definition in the rules that tell it when should it complete. Endless loops never end and can crash systems if not stopped. In JavaScript, the browser will disable scripting when it notices that type of event.
Video Transcript
00:05This lesson is about loops for loops learn about loops
00:08I don't think it's gonna be hard mainly because we've been practicing so much in the last few lessons
00:13So what I want to do next is what I really want to do is I want to take this span
00:18Creation process and I want to turn it into a function. So I'm gonna go ahead and really quickly and
00:24I'm just gonna go ahead here and just really quickly create a function. I'm gonna call it create
00:29Span and it's gonna accept two parameters one is going to be X or let's call it
00:34You know be left top which will be the positions where there it's at and we'll basically just copy this whole thing
00:43Paste it right inside here. And instead of using 50 pixels here
00:48I'm just gonna go ahead and just send that left to the left top to the top
00:56There we go
00:57now what I want to do and obviously if we create our span and we just send here and we just send the values that we
01:03Want it to go to beautiful and we could very easily
01:06Go ahead and create now. Let's say another one by just saying, okay
01:11Now I know where that is, but I want to put another one
01:13That's parallel to it and I want to put at a position 150
01:17and if I do that and I go back to my browser and I click on refresh and I should see here two items and
01:23I'm seeing here that have a little reference error
01:25Span is not defined. So let me go quickly and see what
01:29Well, my problem here is cuz I didn't I didn't copy the tween into there as well
01:33So I'm just gonna make sure that I paste that tween otsan side there go back and refresh
01:38And we should see now two items coming in at the same time
01:43Perfect. Now what I really want to do is I want to create a loop and a loop basically enables me to call our
01:50Code more than one time if I wanted to call this function now ten times
01:55Wouldn't be wonderful if I had some sort of a mechanism some sort of a way to call it ten times and there is
02:02Now the way I do that I basically use the command for and in my command for there's a few rules here
02:08I start with basically defining a new variable and setting its value
02:12The next thing that I do is I say oh, I didn't give you a name right var I I'm just literally
02:17I'm putting my variable right inside there
02:19I'm defining a new variable inside of my loop
02:22By the way, the scope of this variable is throughout the function. So if you're using I again
02:27Don't or you know, don't redefine that I because you're gonna get confused
02:32The next step is we wanted to find for how long basically what rule for how what rule with will this loop go for and what?
02:39I'm saying here is in other words. I'm saying as long as I is smaller than ten then keep going on
02:44The next item I'm gonna do is I'm just gonna upgrade my eye and notice what I'm doing here
02:49We haven't seen this yet, but we've seen I equals I
02:54Plus one. Oopsie. I plus one and we said that I
02:59Plus equals one is the same thing, right? And now I'm telling you that I plus plus basically means that as well
03:05So it's automatically adding one to our eye with just one less character. All right
03:10So I'm just gonna comment that out. And what are you doing here is we have here and I'm just gonna wrap this
03:16Create this just really quickly and then we'll talk about the steps involved here now
03:21We're gonna have to change a few things
03:22So first of all when we're looking at this thing we have your first step is we define I to be zero
03:28we then say as long as I is smaller than ten stay in this loop and
03:33Grow I each time by one. So in other words right now we're at zero. We'll go in do the command
03:40We'll go back up to the top and before we start we're automatically gonna upgrade our eye to be one
03:45now one is smaller than ten and then it'll do it and then two and so forth until we reach that a value where I is
03:52Not that does not match this rule which means when I will turn ten
03:56Which basically means from zero until nine which will be ten times now
03:59We're not going to really see anything yet because we're positioning our element always in the same place
04:03so I'm going to go ahead and just duplicate our eye by
04:07I'm just going to take your and duplicate our
04:10Exposition by eye making sure that I'm always pushing myself a little bit further down the line
04:16And if I save this and I go back into my application, we should see now that our our left position
04:22First of all, we're it's so hard to see it. So what I'm gonna do is I'm gonna add more spacing
04:26I'm gonna go ahead and I'm just gonna wrap this 50 because 50 is also the size of our box
04:32So I'm gonna go ahead and add a little bit more volume to this
04:36So I'll add 25 to it. So we have your 75 each round times I
04:41now what's that what that is gonna do is it's gonna make sure that we're pushing ourselves each time a bit further and what that
04:47Means also is that we're gonna start from position zero
04:50All right
04:50So we're gonna save this and if we didn't want to start from position zero by the way
04:54we could have wrapped this whole thing around and then said here plus 50 if we wanted to start from a
04:59Margin of 50 making sure that because I the first time around is zero
05:03Then all this would equal zero in the beginning and then when we'll be equal one
05:27It'll be 75 and next time 50 and each 150 each time will grow by 75
05:32Pixels to the left now if I save this and I go back into my application and we click on refresh
05:38We're gonna see that our items are now gonna start off all like with a nice small little spacing
05:44I tell they grow through to the size that they're bound to be
05:48now we could have if we wanted to oto affected oto our Y if we would done the same thing for our
05:53Top position and send in the same type of of styling to our top area
05:59I'm just gonna copy this whole logic scheme or even better to save on processor. I could save it as my you know, like
06:07Like just a value here. Let's say X. I'm just gonna call it X and in the beginning
06:11We're not gonna set anything inside of here and I could then set X's value
06:17To the value that we have right here and then I could easily just send this X twice
06:26instead of calculating calculating it twice
06:30And what we'll get is a diagonal line instead of a straight line like we were seeing before
06:36All right
06:38Now what I want to do next and this is kind of cool, right?
06:42So what I want to do next in the next lesson
06:44We're gonna take a step deeper into loops and I so even before we do that
06:48I just want to make sure that it's completely completely clear. So to do that
06:51What I'm gonna do is I'm gonna add here a console call
06:55And I'll just paste here nah nah and I'll just paste your oto the round that we're in
07:00So I I'll put here a comma and if I save this go back to my application click on refresh
07:05We're gonna see that our nah nah nah is being called 0 1 2 3 4 5 6 7 8 9 very simple
07:11Really all we're doing here is we're just telling our code whatever's inside of here to happen as long as this rule applies
07:18Now you could play with these rules as much as you want if you wanted to you could have said that I plus 3
07:23You could have said you're a different value here. They're the everything here is dynamic
07:27You could have done it opposite
07:28for example
07:29You could have said I want you want to start from 10 and as long as I is greater than 0
07:34now the most important part here is that you don't want to call some create something that is called an endless loop an
07:40Endless loop is a loop that could never exit and when you do that, you're gonna crash your application
07:45Which we'll do that just for you. We'll crash the application and then start in the next lecture
07:51From good from the beginning, but I just want to show you this really quickly
07:55This should be fine because we didn't break any rules here
07:57This these rules still apply now on the other hand and if I'm looking here
08:03You're like we're starting from 10 and as long as I is greater than 0 then it's gonna go through this process and do it
08:09Without any problem now the only thing that oh, and I don't think I saved it because it didn't go in the right direction
08:15It should have gone the other direction. Let's see if I save it
08:19Why do I think it's supposed to go in the other direction?
08:23Because I think this X and let me just output hero to the X value because it doesn't see oh
08:29You know why it's because we're just creating it we're creating it
08:34But we're starting from here and going upwards instead of starting from here and going downwards if we wanted to change the diagonal
08:40Let's change the diagonal just for the sake of our experiment
08:44if we wanted to change the diagonal what we would have to do is we would have to change our our
08:49Top position to be a different value not the same value
08:53So if we if this was our X then for our Y to be going down
08:57We probably would want to set it at a different position where we would start from low no matter what and then go up high
09:05So maybe and I'm just gonna experiment here
09:07I think that it would be if we take our eye and just do ten minus that eye and
09:13Duplicate that value and I'm gonna just put that there as our Y and I think that will switch it around
09:20I'm really just going live with you right now because I just thought about and I thought it'd be cool to experiment
09:26And there you go now you can see how we could leverage it to go to the other direction
09:29All right, so that's it for this one. Oh and we promise it will create an endless loop. So to create an endless loop
09:35I'm gonna just write here
09:37X or Y plus plus now what I plus plus is gonna do is gonna make I grow always and if I is always growing
09:44Then it will always be greater than zero and then this is something that you want to always avoid
09:48This is a big no-no
09:50Because if you do this and you run your application what's gonna happen
09:53It's gonna run and if we clear and we run run this we'll see that it's gonna run and it's gonna get stuck
09:58It's gonna get stuck and you can see how my witchy-washy my wiggly waggly my my mouse is going crazy
10:03Because it could never complete in the next lecture after I stop and restart my browser or oh here we go
10:09And I want to stop script and in the next lecture, you can see that it ran twelve thousand
10:161200 times 1200 times before it stopped in the next lecture. We're gonna continue and learn about
10:22Nested loops, so we'll see you in the next lecture where we nest our loops
