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

The answer is recursive

Throughout the last two chapters we've been practicing recursive functions. This is one of the coolest and least known skills that many developers just don't have. It's true, we've been over using them mainly for educational reasons. In the last chapter, we will cover loops (it would be a great idea to go back to these lessons or source files and upgrade them into loops). Anyways back to our task! Our task in this lesson is to convert our three item animator into an unlimited amount of items animator. I know... pretty cool.

Video Transcript

00:00In a real-world scenario, it's incredibly rare where you actually need to use recursion.
00:06We've seen it already because it's such an important concept, and we're going to see it again now.
00:11And it is complicated, so it's fine if you go out of this lecture a little bit shaky.
00:16As long as you understand everything, that's all I really expect of you from this video.
00:22And I do encourage you to check this out again a few times, because this is one of the more complicated videos.
00:29We're going to try to make it the easiest that we can. Let's get to the last lecture.
00:33We said that we have a challenge, and hopefully you stopped and tried to figure it out on your own.
00:38But this is a very complicated task, and I would be so amazed if you did it on your own.
00:43Now, you're more than welcome to pause and post your answer before you watch it,
00:48because it would be amazing to see how do you actually make this dynamic,
00:52where we could then later on send more than just those three properties with our array that we're sending here,
00:58if we wanted to, and still have it work.
01:01So I'm giving you that second, second done. You didn't click on pause, let's continue.
01:05So with A properties, literally the only place that is not dynamic in a level where we could put more properties here
01:11is really these lines of code.
01:13So what I'm going to do is let's walk through this and try to figure out what could we do to make it a bit more dynamic.
01:19Now, when we look at this first line, really the first line is nothing there that could become dynamic.
01:26But if we look at this, notice how similar this is. This line is incredibly similar to this line.
01:31Really, the only difference between the two is how much delay there is and what the value of X really is and the actual property.
01:42So how about we create a small little function?
01:45I'm just going to call this a prime function, position element.
01:54And our position element is going to accept a few properties. Let's figure out what they are.
01:59So the first one is going to get in the A prompts because we're going to need it.
02:04So I'm going to go ahead and I'm going to put there the A prompts.
02:07The next is going to be the index so we can know which item are we going to animate.
02:11The next thing will probably be the delay.
02:16And what else? What else? What else? What else? I think that's it.
02:19So let's copy this. Oh, and actually the exposition, the current exposition.
02:23All right. So A prop. So let me just go ahead and just copy everything that we have here.
02:28And I'm just going to paste it right in here for a second.
02:32And let's see what we're doing here.
02:34So first of all, let me just clean this up a bit. Here we go.
02:38So first of all, let's look what we have here. So A props, that's good.
02:41Index would probably want to set here the actual index instead of putting the actual value.
02:46We probably will need the padding as well because it's an internal property.
02:50So I'm going to go ahead and just pass through also the padding.
02:55So we're going to because we're going to need the padding.
02:58Next item is well, literally what we have here is we're taking the width of the last element that was there,
03:06adding it to the current position that we're at.
03:09Next item is we're actually going to go to that index plus.
03:15Well, actually, in this case, if we're going with it with this idea of index,
03:18and this would be index minus one, because we want to go to the property that was just before to get there
03:23with to add it to that next value.
03:28And next thing for us, what we want to do is and you know that the more I'm looking at this,
03:33the more I feel like that it makes sense for this to probably be below.
03:38But let's take it step by step here because I'm literally doing it with you without knowing what's going to happen.
03:43So those are the two properties we want to change.
03:46Now, I'm going to go ahead and just test it out by basically deleting this line of code.
03:52And instead of that, we're going to call the position element
03:55and see if when we set the position element, if it works for us.
03:59So we want to send the A prompts.
04:02We want to send in the current index, which is the next one, which is one.
04:08We want to send in our delay, which is zero dot three, I think,
04:12but I'm just going to send the value delay from above.
04:14And we want to send the current X and we want to send in the padding.
04:18All right.
04:19So theoretically, I'm just going to leave this the way it is.
04:22Theoretically, this should work.
04:24And I'm just going to comment out this last piece here,
04:27because let's just do it one by one.
04:29So I'm not going to animate the other part
04:31because the positions will probably be messed up.
04:33So first of all, let's just see that the second element
04:35is going to go into place exactly the way we expect it to go.
04:38So if I go ahead here and I go into my browser and I click on refresh,
04:43let's see what happened.
04:44So definitely first item, second item, both of them are going into place perfectly.
04:48So let's position also our last item.
04:50So I'm going to go ahead here.
04:52The item, the thing that's missing is I need this X again.
04:56So I'm going to go ahead and return this new value.
04:59Return X because we want to return the X value.
05:06I'm going to make sure that my X gets updated.
05:09And literally, the next thing I could do is literally just go ahead
05:12and call the function one more time.
05:14And this time around, send the value of two.
05:17So theoretically, this should now work perfectly without any problem at all.
05:22So if I go here into my browser, click on refresh,
05:25the item should all work the way we expect it to work.
05:29And it sure does.
05:30Now, obviously, the only thing that's missing here is because we have here
05:34an extra property that we're animating, which was our margin top.
05:38That margin top property is just not getting into consideration.
05:41But that's fine.
05:42We're going to forgive that because we really want this to become really dynamic.
05:45And we could have sorted that margin top without that animation.
05:48We could have put it in our CSS.
05:50So I'm going to go ahead and delete that.
05:52This is beautiful.
05:53Now, the only problem isn't this is great.
05:54This is exactly what we want.
05:56The only problem is that we're still only animating three things.
05:59If we look into this and I just let's say I give an ID for our label as well.
06:05And I'm going to set an ID for my label.
06:07And literally, I'm just going to call it, let's say T name label.
06:11And I'm going to go ahead and do the same thing here for this label.
06:16It's going to be T date label.
06:19And I'm going to save that.
06:20I'm going to go right back up there to where we were sending this array.
06:23And we're just going to add them in.
06:25So we started the name block, date block.
06:28Those were the properties that we were animating.
06:30But instead of animating the blocks, what I want to do is I want to animate the name and the label.
06:36So I'm going to go ahead there and just change in our array to be sending in the T name label.
06:42I'm going to go ahead to the next one.
06:44And I'm going to send in also the T name itself.
06:47And I'm going to do that also for the T date label.
06:54And last but not least for the ones that we're changing is we're going to approach the date itself.
07:00So now we have your five properties.
07:02Instead of having three properties, if I just run this theoretically, only these three first items would animate.
07:08And we'd be completely ignoring the last two.
07:10So if I go ahead and if everything works according to plan, then we should see.
07:29And we have a little typo here.
07:31So let me see what is going on.
07:36So first of all, A is undefined.
07:38It's telling me, which is basically not really helpful for me.
07:40This is a jQuery problem.
07:42So I'm going to go and look here on my lines to see if I did something that might be wrong.
07:48Did I break a line here?
07:49No.
07:50All this seems to be appropriate, but maybe one of the names is not the way it should be.
07:57So what I'm going to do is I'm just going to this is a good practice to debug in general.
08:01I'm just going to click here and just right here debug.
08:04I'm going to right here return.
08:05Now return is a really great way to stop everything in line 40 and not continue onwards.
08:10The reason why I'm doing that is I want to see am I getting an error at this stage?
08:15And the idea behind this is to be able to basically step ahead until you find where that error is
08:20whenever your error is just not descriptive enough, just like in this case.
08:24So we could see that our error is still going on.
08:26So it seems like it's a real problem.
08:28So I'm going to go back in here and I'm just going to comment out even the call to this property
08:33just to see if that would help.
08:36So I literally even without calling it to see and if that's still a problem that we're going to have to literally
08:41go line by line and try to find where is our problem.
08:44So I'm going to go here and just click on refresh.
08:47And we can see that now there is no problem, which means to us is only after we call this property
08:53and something bad happens.
08:55So let me go and try and put this return like the first thing that the function as soon as it starts,
09:00I'm just going to close it up to try to see.
09:02Am I still getting the error?
09:04And I'm going to just click on refresh really quickly.
09:07And I can see there's definitely no error yet.
09:09So I'm just going to take this and I'm just going to chop it down to be just right here just before
09:14where we saw there was an error.
09:16Save it.
09:18And I'm going to go ahead and refresh it again.
09:22And there's no error still.
09:24But as soon as I go down, as soon as I go down right below line 40,
09:28as soon as I call my tween max set interval and I send all my array of elements in,
09:33that's exactly when our item triggers an error.
09:37That's how it seems.
09:39And that's enough for us to tell us that the problem lies in that line.
09:43So now that we know that the problem lies in this line and we know we didn't change anything
09:47besides the value that is coming through in a props,
09:50it means it must be that T name labels.
09:52One of them is just not right.
09:54So I'm going to go ahead, not T label, but these properties in general.
09:57So I can see T name label T name.
10:00So I'm going to go ahead and just like paste them one next to each other.
10:03T name.
10:04Well, this is just too easy.
10:05Oops, too easy.
10:08This T date label looks good to me.
10:12T date looks good to me.
10:14But an Alina analyze looks good to me.
10:17So it's I'm finding it incredibly hard to figure this out.
10:21So what I'm going to do is I'm only going to send one item in or actually we have to send three.
10:25So I'm going to send these three items in common to basically finish my array right here
10:30and comment out all the other three because I want to see if these three items are generating the error.
10:35And literally, that's why we I'm going to literally testing it slowly, slowly.
10:38And I already see my error and I'm going to talk about it right here, which I didn't put that ampersand.
10:43But that's exactly how I'm going to find the error by slowly dissecting it until I find error.
10:48And now that I'm looking at those three and I already see the error.
10:51But if I would go back into my browser, click on refresh, still see that error.
10:55Then I would be I would be focusing even further in the saying, OK, there must be something wrong in these lines.
11:00But now that I see I forgot to put the ampersand there for the idea that solves my problem, hopefully.
11:06And we could test it out one more time.
11:09We could get rid of that return because that was just for our testing.
11:12And if we read it one more time, we should theoretically just get the first three items animating in.
11:17And that's exactly what's happening. Beautiful.
11:20So let's continue on and solve this before we wrap up this lecture and to be able to solve this because everything is working really according to plan.
11:27We're doing exactly what we need to.
11:29The only thing that I would do is instead of having this call here, I would do a recursive call.
11:34So in this case, basically what I want to do is once my X value comes back, I want to basically ask.
11:41And I'm just going to comment this out for a second.
11:43I'm going to ask here if a props index plus one. Right.
11:56And that basically means that if this item exists, if there's something in here, because if nothing is in there, it would return nothing or in programming, that's called undefined.
12:06So if something is there, then only this value would come true.
12:09In that case, I want to call myself again and I'm going to call myself and send that position element again.
12:15So literally what I'm going to do is I'm just going to call the position element and I'm going to send to it a props index padding and all that great stuff.
12:24And I'm just going to send it the index plus one because we're going to step one further in the index.
12:29And literally just by doing this, it even nullifies our need to send back the X value because we're not even using it because we're sending it into that in a recursive way.
12:39So literally we could basically abandon the X value completely, delete all this stuff because what's happening once we got the X, we update the X, we position our element and then we check if there's another item, then call this function again.
12:54So then it's going to do it with the next index and then it's going to ask in the next index because this index is each time is going to roll up even more.
13:01So you could send 100 properties or three properties and it doesn't really matter because it should in theory at least recursively animate all of them until all the items are in position.
13:10So let's see if that really worked.
13:12And really our only problem left is that we're always sticking to the same delay, which is probably not what we want.
13:19So if we run this again, we'll see that all the items should be more or less animating at the same time, which we might be fine with that.
13:25But if we want to stick to our rule that we said with our delay, then we probably want to each time add the value into the delay in some way.
13:33And the way we would do that is we will just go to the delay and literally just duplicate it.
13:38Duplicate it, which is where are you, Mr. Duplicate?
13:41I just can't see my keyboard. I'm in the middle of the dark here.
13:45Bear with me. All right.
13:46Here we go. Here's my duplicate and I'm going to duplicate it by the current index.
13:51And what that would do is it's going to make sure that it's going to upgrade the delay by time's amount of our current index each time.
13:58So if I save this and run it just one more time, we'll see that now everything is going to animate in and we're going to have our delays that we were hoping to get.
14:06Just want to run more time. Beautiful.
14:10All right. So now we're getting that segregation that we hope to get.
14:13And that's it. That's all the new stuff that we're going to learn about in this chapter.
14:17We learned about arrays. We learned about objects.
14:19We've worked with functions, much functions, much more deeply than we ever have before.
14:23And we really have done quite a lot. This chapter is not over because we said that we're going to talk a little bit more about practicing objects and arrays by actually figuring out a way for us to get analyzed data by clicking on this button.
14:36And we're going to start doing that literally in the next lesson.
14:40And we're going to take advantage of working with associative arrays and arrays and that will be our last practice about this topic before we move on to our next chapter.

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!