02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Working With Arrays

In this video we look into how we retrieve data from an array.

Video Transcript

00:00Before we learn new concepts, I wanted to have a little rehearsal over the concepts that we already met.
00:06Now, one thing before we could do that, I added here two arrays, one with the amount of months,
00:10well, basically the months of the year, which would contain 12 different months,
00:14and then with seasons, which contains 0 to 12 seasons that are correlating to the months,
00:19just basically letting us know based on the month what is the season.
00:24Now, the one thing that you're noticing is that there's a horizontal line here going on,
00:28and if I wanted to make sure that there's no scrolling, what you could do is you could go up to your view,
00:34and in your view panel, just select Word Wrap, very similar to any other editor that has copy in it.
00:42With that said, usually, even though I set my Word Wrap, things get a little bit messy
00:47when you just let things break lines on their own.
00:49And the nice thing about Flash is that sometimes you could just break a line,
00:52and it doesn't really care because it knows that you're in these brackets.
00:56And as we talked before, Flash knows that it's expecting a closed bracket before it's over.
01:02And because it's expecting something to happen before, we could use that to our advantage.
01:07And let's just have here that every four months, we're just going to break a line.
01:10And Flash really doesn't care that you broke a line because it's smart enough to actually know that
01:15all this is actually one line for Flash, okay?
01:19And same thing we could do for our months just to make it a little bit cleaner and easier to view.
01:25So that was just a small little note about just cleaning up our code and making it more presentable.
01:31So now we have our new array of our months, and we have our new array of the seasons.
01:35Let's see how we could approach that data.
01:38First of all, let's just double check and make sure, even though we could see it now visually because you broke it down,
01:43let's just check to just confirm that we have a correlation that the exact same amount of data
01:48we have in our months array is the same amount of data we have in our seasons array.
01:53So I'm going to just type a months dot length, as we met in our first video,
01:57which tells us how many items are in our array.
02:01And notice that our length is not a method, it's a property.
02:05It's a property that knows, so it's basically like a variable that knows how many items we have in our array.
02:12So it's important to know that if we type length open and close bracket,
02:16we would not get what we would expect, because our months is expecting for a property.
02:22And you could see that in your hints a lot of times, the difference between a property and a method,
02:26a method, you'll see those open and close brackets, and it will show you the different data that it would expect.
02:31While a property would just show you straight on just the value and tell you what type the value is without any open and closing brackets.
02:39So I'm going to type my a months, and I'm just going to check if it equals to my a seasons, or a season dot length as well.
02:48If it is true, then we should get the value of true in our output.
02:52And we could see that we're getting the value of true.
02:55The next thing that I want to do is just make sure that it's actually 12.
02:58So I'm just going to type a months.
03:00Now that we know they're both the same, we could just check the length itself.
03:03So I just put here a little comma.
03:05And if I trace it out, we should see that it's true, and there are 12 months in the year, and that's exactly what we wanted to see.
03:13So let me just comment this out.
03:15And let's look back again at just the basic things that we've met so far.
03:19And one of them is we said that our rate starts from zero, it does not start from one.
03:25Thus, if we wanted to get to January, or we wanted to get to winter, we could have traced out, first of all, a months.
03:31We open our bracket, type zero, close bracket.
03:35And if we just trace this out, we should retrieve January.
03:39Now, if we wanted at the same time to retrieve those our seasons, we could just put here a little comma.
03:44And put here an a season, open bracket, type zero, close bracket.
03:51And again, we should retrieve the first item, which is the index zero, and it should return the value of winter.
04:00And you see that January is winter.
04:03Thus, if we wanted to make it a little bit more palatable or friendly to the eye, we could say that is in.
04:14Now, obviously, if I run my application right now, we're going to see that we're going to get an error.
04:18And as we said before, first of all, the reason I'm getting this error is because I didn't write the right syntax.
04:23It doesn't really know how to connect these two.
04:26Next, we have two ways of working with our trace or by putting a comma, which basically means that we're adding more parameters into our trace.
04:34Or if we were working in action script two, or even three, we could just combine these characters altogether just by using the plus sign.
04:42We could combine all these characters and create our value, which is one parameter.
04:48Trace in action script two accepted only one parameter versus action script three, which it accepts any amount of parameters.
04:55And now we could see that winter is in, well, January is in winter, and we could see that we should add your space before here.
05:03And after our end, you run it one more time.
05:05And we can see that January is in probably the winter, right?
05:14Perfect.
05:15So now we know that January is in the winter.
05:19Let's see how do we access now our last item in our array.
05:23And let's look at it first by just typing manually.
05:26So we know that we have 12 items in our array because we traced it out earlier.
05:31But what if we didn't know, even if we didn't know, we could leverage the fact that we have our length property.
05:36So if we wanted to, we could have traced out a months, open bracket, close bracket.
05:42And in this bracket area, what do we want to do?
05:45Oh, let's make it a little bit more fancy.
05:47Let's before we even do this, let's just grab our tracer because we're going to be reusing this.
05:52Let's just put it into a function.
05:54So we're going to call it.
05:55We're going to create a function that says, we'll say output month, output month, which would accept one value, which would be an integer.
06:08It would not return anything.
06:10This is great.
06:11And we're leveraging our knowledge of functions.
06:14So now we could actually, instead of outputting directly because we want the same value, we could just automatically just put here this value, right?
06:23We're using our function.
06:24And we can now trace our function, or we don't even need to trace because we're tracing already right here.
06:29So we could just call our function.
06:31And as we call our function, let's start with calling item zero.
06:35And let's just confirm that we're still getting January, January is in the winter.
06:40Now that we did the first one, let's leverage the fact that we know the length of our array.
06:45And let's send our function the last item in the array.
06:50So as we said, if we just go to a months dot length, we got the length of our array.
06:55But if we run this, it's not really going to give us what we're expecting.
06:58It's going to tell us undefined is undefined, which is not what we want.
07:02What we want is actually length minus one because our length equals 12.
07:06But as we said, our index starts from zero.
07:09Thus, if we call our output mode and we call length minus one, which basically is 11,
07:14we send it to our output, then it would output month 11 and season 11.
07:18Thus, it should create for us exactly what we want, which is December is in the winter.
07:24And just so we could make sure that we get this, let's just output one random month in the middle
07:29and see that we're actually getting, let's just try output month six, which we know exists.
07:38Let's say we have your little error, possibly undefined method.
07:41We'll see that it must have your some sort of spelling error.
07:44Because you see, I actually made a mistake earlier.
07:47I called it output.
07:49Let me just rename our function to output because it's probably more logical
07:55and run our application one more time.
07:58And here we go.
07:59July is in the summer.
08:02Perfect.
08:03So now we see how we could leverage the fact that we have arrays and how we could dynamically
08:07create connections between things and change things to the user.
08:11So if you think about it connecting it into your application, arrays,
08:15they don't have to be connected to seasons.
08:17They could be connected to anything that there's a direct connection to a number, an index number.
08:22Thus, it could be the days of the week.
08:24It could be anything that really has importance in levels, such as levels of membership
08:30or levels that are, you know, like level zero, level one, level two, and so on.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!