Nesting loops to create grids
Nesting loops is probably one of the coolest things you can do with loops. You can create really cool animations that take advantage of the full screen such as creating a grid on it with nested loops. There are many other benefits of using nested loops, but let's start with that and keep your mind open to opportunities where two dimensions would become useful.
Video Transcript
00:00nested loops. Nested loops are loops that are inside of loops just like we saw with
00:05if conditions that they could be nested, that's the case also with loops. One of
00:10the biggest advantage of nesting loops is enabling us to create two-dimensional
00:14or even three-dimensional art. When you think about it, if you have a loop
00:18inside of a loop inside of a loop, you could play around with values such as
00:22z, x, y, z. Or if you have a double loop, you know, a nested loop, you could play
00:28with x and y and then create a grid. And that's exactly what we're going to do in
00:31this lesson. We're going to create an art in a grid. So let's right continue. What
00:36I'm gonna do now, I just want to comment everything we've done so far, leaving that
00:40function because we're gonna use that function again. I want to show you how
00:44we create a nested loop. And nested loops are really, really useful. And I'm just
00:49gonna create something really, really quickly. Create the same thing. We have
00:52your var, var i. I'm gonna set it to zero. I'm gonna say as long as i is
00:57smaller than 10, I'm gonna set it to be make sure that my eyes always growing by
01:01one, making sure by the way that my array, that my loop, sorry, was will
01:07always run legally. In other words, it's not going to create an endless loop like
01:12we saw it in our last sample. Next thing what I want to do is I want to create
01:17another loop right inside of there. And I'm going to call it just a different
01:20value. So for i, we'll do j and j. And this is something a lot of developers
01:29make mistakes in. When you're using i and j, don't use my mistake i for j or j for
01:35i. Now what we're doing here, if I if I if I log console dot log, it's gonna be
01:43a little bit hard to realize what's going on in the beginning. But don't
01:47worry, it's gonna get easier. I'm gonna log both values inside of this
01:51loop. Now, just saying what's going to happen, when i equals zero, first round,
01:58it's going to go to this loop and do this 10 times. And then i is going to go up
02:03into become two, and then it's going to go do this two times. So what we're
02:06bound to see is that i will remain at zero, while this is changing 10 times,
02:10and vice versa. So if I go, go to my application again, and just look at the
02:15output, we're going to see that we're going to get that value that starting
02:19from zero, our j is going to grow, and then and then our i each time is going to
02:23grow, can say, you know, first, first that j is going to grow each time 10 times,
02:28and then reset each time because we have a loop inside of a loop. Now what this
02:33enables me to do, it's a really great way, and that's why we started from the
02:37span. It's a really great way to create tiling. And if I wanted to tile
02:42everything in my application, literally creating, if I could just undo this
02:46for a second, literally creating here, a mosaic, because what I want to do is I
02:50want to take my my tiles and position them one after each other. Now I know that
02:55my tiles, their size are 50 height and width. So you know, like, let me just
02:59comment out this animation for a second. And really what I want to do is I want
03:05to position my elements each time in position basically 50 plus left over
03:13plus 25. So really, I want to I want it to be 75 each to create a bit of a space
03:18between each element. Now I'm not going to put just 75 75 in both of them. What
03:23I'm going to do is I'm going to use the I I'm going to duplicate my my top
03:27position, my my left position for by I making sure this will grow my from
03:32right to left. And I'll use my J to grow from top to bottom. And that's
03:37going to make sure that I'm going to cover the whole box of the content that
03:41I'm trying to get to. So if I go back into my application and click on refresh,
03:45we're going to see that all our boxes are going to be positioned exactly the way we
03:49would hope for them to be throughout our page. Now, the last thing that we really
03:53want to do is we really want to create only the amount of elements based on how
03:56much screen we have, figure it out and calculate that out. To do that, I'm
04:03going to go back into my browser. And you know, let's actually do it in the
04:06browser. Really, what I want to do is I want to basically approach and get the
04:11actual width of our document. So to do that, what I'm going to do is I'm going
04:15to approach my my jQuery, wrap my document with the jQuery object and
04:22ask for the width. Now it's going to give me the actual width of our
04:25application, and I could do the same thing for our height. And this will
04:29be the actual width and height of our document. So all that's left for me to
04:34do, the visual document, all that's left for me to do is to go ahead and leverage
04:38this value that we got back, go back into my editor, and I'm just going to go
04:42here and just create here a new variable. I'm going to call this total
04:48width. And I'm going to set here basically again, taking my document,
04:53asking for the width, which is a function, what I'm going to do is I'm
04:57going to already automatically divided by 75, because I want to get to that
05:01actual number of total, total item, total items with, and what I'm going to do is
05:10I'm just going to turn it into an integer or parse int, making sure that
05:16the value will be an integer, or if you're more comfortable using the math
05:20floor, that we want to make sure that we drop everything, we're not adding any
05:24values, or are we because you could always if you want to just add one more
05:30item, no matter what, so we'll drop the floor, and then add an extra item for
05:34safety. Now that we know the total items with all that we have to do is basically
05:39go ahead and tell to our I don't grow until 10. Instead of growing until 10
05:44grow until whatever that amount is. And I have no idea what that amount is, but
05:48we could hear have your little trace out and find out what that is. If I go
05:57back into my application and click on refresh, we're going to see that now
06:00we're going to get the full span and we can see that extra little bit there,
06:03which is actually really helpful. You can see that we have 18 here. And
06:07literally, that's all we had to do to make our items take over the whole
06:11screen. And if we wanted to really if we did it, if we didn't want to have it
06:16all the way going all the way down here, and we only wanted it to be the
06:19visual screen, we could have gone ahead and created the same exact thing also
06:24for our height. So let's do that total items height. And I would basically do
06:31the exact same thing. Only instead of instead of that, we would approach the
06:37height. And by the way, that's because we're approaching the actual document.
06:43And we'll talk about that further in a minute. And if I go ahead and I just
06:47refresh this and I go back into my browser, we're going to see that now
06:51our item is going to go all the way based on how much our length is and
06:55not and not Oh, we actually didn't change it. Sorry. I updated the value,
07:01but I didn't change how far could J go to. But now when we save this, and we
07:05go back into our browser, we'll see that it's going to actually come back
07:08and based on how much our height is, you can see so basically based on
07:13the size of our document right now, it's basically outputting our elements.
07:18So if I went ahead right now, and I actually move this down, you'll see
07:21that because this was here before, it wouldn't grow all that way. But if
07:25I made it a little bit smaller now and refreshed again, we would see that
07:28it would grow again into that size. Okay, so that's it. All that's left for
07:32us to do really is for to make it really, really interesting is to bring
07:35our animation back. And I deliberately did it this way, because I thought
07:38it's a really cool transition that you could create on your site. When
07:42someone clicks on something, you could have it basically go, you know,
07:46fade out and then move on to the site. And then when you're on the
07:49actual page where you want the actual animation to happen, or you
07:53want the actual fade out to happen, you could literally just go ahead
07:56and basically do a from and then scale it to zero.
08:04It actually you have your from two, I believe, and then you can have your
08:08scale two, and we could set it from two
08:11to zero. And then if we go and we click on refresh,
08:18we'll see that now we're going to have this really cool transition going on.
08:23So there you go. That's it about four loops and nested four loops.
08:27We're still going to be on the topic of loops for a little bit further, but we're
08:31getting really close to the end of this course.
08:33I just want to talk about a couple other ways of creating loops.
08:36We're not going to dig too deep into them. I just want you to be familiar
08:40with them. And really, the next lecture will be our last lecture
08:44for this course. So that's it. See you in the next lecture where we wrap up the
08:48conversation about loops.
