Nesting Loops to create a Grid
In an earlier title Conditionals and Loops we discovered what loops are and how to work with them. In this part we are going to learn a few new things about loops. Followed by creating our first nested loop. The idea behind a nested loops is to be able to create a loop inside of a loop. This type of strategy is sometimes called a 2 dimensional loop. In the core of it is just two loops only because one is inside of the other each time the first loop finishes a round the internal loop does a complete loop.
Video Transcript
00:00But, in our case, what we are going to do is we're going to create quickly a for loop
00:06inside of a for loop.
00:08We're basically going to create one for loop that's going to be in charge of the width
00:12and one for loop that's going to be in charge of the height.
00:16And to do that, we're going to basically, we're going to, I lost the word, we're going
00:20to nest.
00:21Ah, there we go.
00:23We're going to nest two for loops, one inside of each other.
00:25Alright, so first of all, we're going to start and we're going to create a variable.
00:28I am going to call it x, so I remember that this value is going to be reflecting our x
00:37values.
00:38And I'm going to set it to be zero initially.
00:42I'm going to make sure that my x is always smaller than my width count.
00:49I'm going to also then make sure that I step x value one step up each time I do my loop.
00:56So now we got our width loop.
00:58Now what's left for us is also we need to create also our y loop.
01:01Alright, so it's time for us to worry about our height loop.
01:07And for our height loop, we'll do a very similar loop, but this time around we'll start with
01:11our y.
01:12And as long as our y is smaller than our height count, then we'll progress our y.
01:20And all that's left for us is to add the magic that happens right inside here to make
01:25all this actually start drawing things.
01:27And what we want to do is, and really what we want to do is we want to randomly select
01:32a color each time we draw a circle.
01:35So to do that is very, very easy.
01:38What we want to do is we want to do is we want to go to our context.
01:42And in our context itself, what we want to do is we want to fill style.
01:48We want to set a style for our loop, for our color.
01:52We want to pick a color from our a colors.
01:56What color do we want to pick?
01:57We want to pick a color, any number, between 0, 1, 2, 3 in our case.
02:02How are we going to figure that out?
02:03First of all, we're going to want it to be, we're going to want to use the math random.
02:09Math random returns a number between 0 and 1.
02:12And we want to duplicate that by our a color length.
02:15Now the number that we're going to get back is going to be a number between 0 and a color
02:20length.
02:21But it's going to be, oh, math is with a lower a, not an uppercase a.
02:27And what we want to do is we want to make sure that we're going to get this value back as
02:30an integer or as a number that does not have a floating variable.
02:34So to do that, we're going to use again the, well, we didn't use it before.
02:40I thought we did.
02:41All right.
02:42So here we go.
02:43Oh, yep, we did.
02:44Parsons.
02:45So that's what we want to use.
02:46We want to use parsing, which is going to get rid of anything that's floating, which
02:49is basically right here.
02:52So if I just, if I say it one more time now that we have it drawn, what we're actually
02:56doing is we're finding a random number between 0 and 1, which is math random.
03:02And then we're duplicating it by our number, which is basically in our case 4.
03:07So we're going to get a number between 0 and 4.
03:10All that's left for us to do now is to make sure that this number is going to be without
03:15a floating variable.
03:18So we're going to parse it into an integer using the parse int function, wrapping this
03:23all together, and now we're going to get a value 0, 1, 2, 0, 1, 2, or 3.
03:30And 0, 1, 2, or 3, we're going to use that to index and find whatever color we want.
03:34So there you go.
03:35So it was a little bit complicated, but line 25 defines a color for us, and each time the
03:40loop runs, it's going to get us a different color, or it's going to randomly select
03:45a color.
03:46Now, next item for us to do is when we're drawing with a canvas, we want to, there's
03:52a few steps involved before you could draw, really a complex shape, is you want to begin
03:58a path, you want to close a path, when you open up a path, then you start drawing, then
04:03you close that path, and then you only fill it.
04:07So it's kind of like the steps are you first tell JavaScript what you want to draw, and
04:12when you're done telling it, then you say, OK, now you can draw.
04:14So let's do it.
04:15It's quite easy.
04:16What we do is we call the function beginPath, and it's very similar to the way it works
04:21also in ActionScript.
04:23And then context, arc, is a function in JavaScript that enables us to draw things, and basically
04:30what we want to do is we want to start with our position, and our position would always
04:36start from, because our circle, we always want to start with kind of like an offset
04:41of our radius, because it's going to be basically our arc.
04:44We're going to ask for our x, y value, our radius, and our angles, which I'm going
04:50to explain how that works in a second, all right?
04:53So let's see this.
04:55What will our x value be?
04:57The x value that we want is basically we want to start from our radius, because our circle,
05:01if we start at 0, 0, we'll just get like a moon, a half a circle, and it's not what
05:05we want.
05:06So we're going to start from the radius itself in our x to make sure that we get
05:11the full circle.
05:12So I'm going to take that radius, and then I'm going to add to it our amount
05:16of gaps currently, well, our total amount of gaps, so our size of a circle, times
05:22our x.
05:23So when our x is 0, there will be no gap, so then it's just going to be a space
05:28of a half a circle, enabling us to draw a circle, and then each time that the
05:31value will go up, we will then continue on and get to our drawing point, okay?
05:39So next, we want to do the exact same thing for our y, only changes that we're
05:44going to use our y for our y position.
05:47Our next step is we want to use our radius, the same exact radius always.
05:51The next step after that, which is a little bit tricky, and we're not going to delve
05:55too deep into this because it's starting to get a little bit complicated, but basically,
06:00the way the arc, arc doesn't create only complete circles, it creates arcs.
06:05And what we want to tell it, we want to tell it from what degree until what
06:07degree do we want to draw.
06:09Now if I would tell you that the first degree is 0 and the last degree is 360,
06:13you wouldn't have any questions.
06:15But actually, I'm going to tell you that the first degree is 0 and the second
06:18one is math.pi times 2, which if you're good in algebra and you remember this
06:26from high school or preschool or whenever people learn about this, a circle
06:31diameter is math.pi times 2, which is the same thing as 360 degrees, only in
06:37computer language and many programming languages, computers prefer that we
06:42speak to them in radians.
06:44And basically, math.pi times 2 is 300 degrees in radians, OK?
06:53Well, I'm glad we solved that one.
06:57And last but not least, we're going to set here the next value as true.
07:01Don't worry what it is, just set it to be true.
07:03We don't want to get stuck in every word here.
07:06context.closePath, so we drew our arc, we close our arc, and if we refresh
07:14now, we won't see anything, so we're going to have to do one more thing.
07:17We're going to actually need to ask it to fill out our element.
07:21Now, by the way, the only reason why we're beginning and closing our path
07:24each time is because each time we're changing a color and we want to fill
07:28before we change a color, again, because if we begin and close, begin
07:31and close and only draw or fill later on, we're actually going to color
07:35everything in the last color that was inputted in.
07:38Or things could get confused in general.
07:41So basically, each time that we ask to begin a path, we're asking for
07:45a completely new fresh start and a new fresh end.
07:49That way, it's very clear what we want to draw, where it starts,
07:52and where it ends.
07:54All right, now if we run our application, let's see if what we did
07:57is working, and hopefully it is, and there we go.
08:00So we're already in a much better shape than we were previously.
08:03Now, if we refresh this one more time, we'll see that the colors
08:06each time change and they move around.
08:08Our only problem is it's not refreshing, but lucky for us,
08:11the refreshing part is very, very easy.
