24 minutes of doing
It's time for us to practice again. Over the last few chapters, we learned a pile of new things. Learning new things is just not good enough - it's about the practice. As we practice we'll be learning some new things. The biggest take out of this lesson is our capability to leverage primitive data types and how we can fetch from them extra information through their built in methods.
Video Transcript
00:00Okay, time for me to blow your mind. Remember when we said that variables are the primitive variables, like numbers and booleans are just tangible things, they're not objects.
00:11And that's why we can't really, well, they don't just, they don't do more than just, they're just whatever they are, they're a number, they're a string.
00:19Contrary to an object that could be a huge container full with a lot of goodies.
00:24So the mind blow is that even numbers and strings and booleans have properties and even functions.
00:33Now, it sounds insane because it is insane. It's not logical. It's not meant to be logical.
00:40Actually, there's many programming languages where numbers are just numbers and that's it, and strings are just strings and that's it.
00:46But the idea behind it is instead of you having like a separate library to find out how long is your string or a separate library to find a specific character in the string,
00:55it's kind of bundled up inside of the number and the string as if it would, well, as an object.
01:00It's just an object that is a primitive object. So even if you just write the number, it automatically has that value in it.
01:06It's still all the rules and all the stuff we said completely still apply.
01:09But now that we're more comfortable with numbers and objects and the difference between them,
01:14it's time for us to see them in action. And that's exactly what we're going to do in this lesson as we continue to try to make our function anonymous to us.
01:23In other words, we're trying to make this function reusable, where there's nothing hard coded inside of it.
01:28So we could reuse it, for example, if we had a few lines. All right, so we're almost there. We're getting there. Let's continue.
01:34Things dynamic really made almost everything here dynamic. The last things that are left are really small items such as the delay, the margins, and really mainly this margin top here.
01:48So what I want to do is I want to first start off with talking for a second about how do you approach and how do you get the current margin from our element?
01:58For example, let's say in our background, which it's not true right now, but let's say in our paragraph, if I find it here,
02:04imagine that we had here a margin top and imagine that margin top was 30 pixels already.
02:11Now, if I set here a margin top and I change that margin top to 35, that's fantastic. But what if the margin here was even 50?
02:20Maybe the user really wanted their margin to be really big. And now when I run my application, my margin is actually going to get smaller than what it should be.
02:29And that's not what we want. We don't want our margin to get smaller. We want to make our margin, let's say, bigger by a certain value.
02:36So to be able to do that, and I want to go back into our code, we want to really fetch the current margin.
02:42And I'm going to show you how to do that. It's incredibly simple when you use jQuery. So I'm going to go here into my console, log, and I'm just going to log out that value.
02:51And the value is going to be inside of that dollar property again, because we want to approach jQuery.
02:56And we want to say what property do we want to get that information? We want to get it from our bottom item, so we don't even have to put here a string because it is a string.
03:04And I could literally go ahead and ask for the CSS property and literally just request from it the margin top.
03:12And I'm sending it in as a string, but I want to make sure that I'm lower casing it. So it's margin top just this way.
03:19So if I save this and run this, and sorry if you hear a little bit of noise in the background, hopefully that will stop in a moment, but I can't control it.
03:27And if we go into our console, just to peek at it, we'll see that we have here that 50 pixels, which is the value that we have inside of our CSS.
03:36So now that we have this value, I can literally just take it, convert it into a number, add the value that we want to add into it, and then turn it into whatever value we want.
03:46So let's say we want to add five pixels to whatever it is. To do that, what I'm going to do is I'm going to go ahead and I'm just going to create here a variable.
03:54And by the way, we're only going to do this if we're in the bottom item, so I'm going to open a curly bracket and close it.
04:01And I'm going to go ahead and inside here, I'm going to create a new variable, and I'm just going to call this margin.
04:08How about margin top even? That works as well.
04:11And I could literally just take this value, chop it, put it right inside of this variable.
04:16Now, the next step that I want to do is I want to basically strip out the PX out of it because we want to get to be a number.
04:24So to do that, I'm going to use a property that exists in every single string, which is quite amazing.
04:29So I'm literally going to go ahead to that string itself, and I'm going to ask to slice out the elements that we want.
04:38Now, this is kind of exciting because we so far thought that strings are just strings.
04:43But really, strings are kind of objects that we never instantiate. They actually have properties.
04:49So in this case, if I want to take this margin top, I'm just going to console log it just so we could see it in action.
04:55I could go to my margin top and I could say, hey, I want to get the index of the property of PX.
05:04Now, don't forget that the index of is basically going to be the position in the string.
05:10So if our string value is 50 PX, it's going to be this is zero, this is one, and this is two.
05:17So we should get the value to come back in our output.
05:20So if I run this and I go back into my browser and click on refresh here,
05:24we're going to see that we're going to get the value of two indicating to us that there is a value there and it's two.
05:31So they go back into my application and I'm just going to go ahead here in my editor.
05:36I'm going to basically ask after I got this, I'm going to ask, let me if.
05:43If margin top index pixel, that means there is a value here.
05:49If there is a value here, I want to basically go to my margin top and I want to change its value to be margin top.
05:55And I'm going to call the slice property and the slice will take an index, same thing from zero to whatever the length is.
06:02In this case, I want to say, hey, I want to chop everything from position zero all the way to the position where our PX first arrives.
06:09And what I'm doing here, and this is a little bit sophisticated, I know, but basically what we're doing is we're taking that element
06:15and we're chopping out of it the PX because we're saying, hey, I want to slice out of this string.
06:20Notice how we're approaching it just as we approach the objects because really every single variable in JavaScript has properties,
06:28even if it's a string, even though a string is not something that you instantiate, it has properties that you could manipulate.
06:33So if it's a string, we could slice it.
06:35And if it's a string, we could slice it.
06:37And in that case, we could also get the index of something.
06:40We could slice it.
06:41And I'm saying slice a hundred times.
06:43And now if I do a console log, we should now get only the value, which was 50 as our value in there, which is exactly what we hoped for it to do.
06:55So if we go here and click on refresh and we'll see 50.
06:59Now, I want to test something because right now this 50 is a string because it was wrapped around with that 50 PX.
07:04It's not a number.
07:05So what would happen if we try to add to a string, a number?
07:08If I go ahead and I just take the string and I try to just add to it, let's say five and I just add to it inside, look what will happen.
07:16If I just go back into my application, click on refresh, notice what the value that will come back.
07:36It's not going to be 55 as we would hope for it to be because 50 is a string and it's going to take that string and add to it a number thinking that I need to convert that number into a string as well.
07:47And that's why our value would be five hundred and five.
07:50But on the other hand, if we would go back into our application here and instead of adding that as the secondary item, but if I would start with the number and I would start with five plus a string because you have your number plus a string.
08:04And if I go back into my browser and click on refresh, you're going to see that now it's 550.
08:09So it's still not respecting our string.
08:11So it doesn't really matter the direction.
08:13As soon as it saw a string, it automatically concatenated it.
08:16And even if it didn't, then it worked the way you wanted it to work.
08:20You really want to make sure that you're always connecting the same type of things with the same type of things, because if not, you could have unexpected behaviors.
08:28So really what I want to do is I want to take this item and I want to tell it, hey, I want to convert you into a number.
08:35So to do that, I'm going to use the parse parse int parse int, which is a function.
08:41It's a floating function and parse int basically takes in a string and converts it into an integer or a number in our case.
08:49So if I save this and I go back into my application and I refresh it again, we'll see that now when we're combining five with 55, we're getting there 55 as we would expect it to be.
08:59So again, I'm going to go into my browser.
09:01And now that we've seen that I'm just going to take that parse int and I'm going to just automatically just wrap it around that value here.
09:07So that way we're always getting that value.
09:09And notice how it's so critical.
09:11You open a bracket, you have to close a bracket because if you don't, it could get very messy.
09:16And now we could approach that margin top and treat it as if it's a number because it is.
09:21And notice how in variables in JavaScript, just like we saw that in the beginning, we had a bottom top, a bottom item, which was false, which is a boolean value.
09:31We convert it into a string when we send in that third parameter.
09:35Just like that, when we first initially put here a value, it's a string.
09:39But then we take that string, cut it up and convert it into an integer.
09:43And now it's a number.
09:44So if in this line, it was a string in this line, it's a number variables in JavaScript could contain anything.
09:50There is no bounding to it that says, hey, this variable could only be numbers.
09:54And generally you want to avoid using the same variable for a lot of different types of information because it can become incredibly confusing.
10:01But now that we got that sorted out, really, all that's left for us to do is to update our it's to update our margin top to that new margin top that we want it to be.
10:11So literally, I could just go ahead and add to it whatever value I want.
10:15In this case, what I'm going to do, I'm just going to delete this.
10:18In this case, what I'm going to do, I'm going to take that padding variable that we've created earlier.
10:22I don't really know where we created it.
10:24Let's see. Where are you padding that I created?
10:28I was sure I did.
10:29Oh, yeah, the padding is up here, right?
10:31So I'm going to go ahead and I'm going to add that padding into the variable that we have there.
10:36So literally, what's happening here is my margin top is going to be whatever the value was there plus padding, which is 17 in our case,
10:44which in our case would be probably too much.
10:46But let's see what would happen.
10:48So if I save this, all that's left for me to do is to take this margin top and literally just paste it in here and then concatenate it with a string.
10:58Now, notice how in this case, I'm not telling this to be a string because automatically when I add a string to a number,
11:04it will automatically convert that number into a string, creating a bigger string.
11:08So if I save this and I run my application just one more time, we'll see that this time around,
11:12it's going to move down another 17 pixels no matter where it was before.
11:16So if before it was in a position, if before it was in a position and its margin top, let's say, was 10 and we refresh,
11:24we'll see that this time around, it's going to animate into its position.
11:28Now, I want to add a little bit more logic here and a few more things.
11:32One is what would happen if there was no margin top?
11:35What would happen if we just didn't set here any value at all?
11:38So if this was not set at all, there was no value coming in, it's going to break our application right now
11:43because right now we're expecting the value and let me just really delete it.
11:47So I'm going to go ahead here, just completely delete it, save it, and I'm going to go ahead and refresh.
11:53And if we look at this one more time, we'll see, first of all, what is this value?
11:56How did we get to this value?
11:58So let's take a deeper look and see what's going on.
12:01So if I come here and I'm just going to trace out the value that comes back here.
12:05So I'm just going to go here and I'm going to put here a console.log
12:09and I'm just going to log out our margin top as the value is changing.
12:15All right, so that's one.
12:17And if I take this value and I test it out again down here, we'll see what happens.
12:22Again, I'm going to just refresh here.
12:24So we'll see the first time around the value is 18 pixels.
12:28And then the next time around it is 33 pixels.
12:31And really the logic that's happening here is by default there is a margin top to my CSS that was inherited,
12:38even if it's not inside of our current slide because it got it from somewhere else.
12:42So if we look around, we might find it somewhere, see here margin 19 pixels, that probably isn't that.
12:49Let's see, maybe font size 18.
12:55I don't even know where it's coming from.
12:56It could be like a default margin or maybe it's because the font size is 18.
13:01But let's test it out a bit because let me just test it out here for a second.
13:07Click on refresh.
13:08But it doesn't really matter to the point of the matter here is that the default was 18.
13:12So it's adding another value to it.
13:16Now for us, it's working really, really nicely.
13:18But what if, let me just force it to be not as good for us.
13:22So what if our margin here was really like a really low value?
13:26What if we set our margin top to be zero pixels?
13:29Maybe the default was zero pixels.
13:31If we run our application again, we're going to encounter a problem that we're probably not going to like is going to overlap.
13:37And we can't request the person that built the HTML to update it.
13:40So we're going to have to figure out a way to sort this out in JavaScript.
13:43Lucky for you, that was the whole point of this whole rucka-fuffle that I was going through.
13:48So if I go back into my application, really what I want to get is I want to get the height of what, let me just show it to you.
13:54I want to get the height of this element.
13:56So to do that, I want to basically make sure that the height of this element is taken into account.
14:02So to do that, I'm going to go into my element here and I'm just going to console log, log.
14:08And I'm going to log out basically the width of this element.
14:11To do that, I'm going to use the jQuery command and I'm going to send inside of it my bottom item.
14:17Well, actually not my bottom item.
14:18I'm going to send inside one of our items.
14:20Doesn't really matter which one, let's say the left item.
14:23And I'm going to ask for my left item to its height, just like you did with the width, by the way.
14:32So I'm going to go ahead and just output it so we could see it in the console.
14:42And now we'll see that our basic height is 22 pixels, which is less, is more than what our padding is.
14:48So what I'm going to do is I'm going to go ahead and basically say this.
14:52I'm going to go to my application and I'm basically, I want to break these steps a little bit further.
14:57So we first, we got the element itself.
14:59I'm not going to, I'm going to parse it into integer.
15:01That's beautiful.
15:02I'm not going to add the padding yet.
15:03I first want to get the actual value.
15:05The next step is I want to go to that margin top and I'm going to say to it, hey, from now on,
15:11you're worth the biggest number of math.max, which is one function of the math object.
15:17And I'll send it or the margin top or the actual height of one of the properties, which is this baby right here.
15:25So what I'm doing here is I'm basically saying, hey, by default, no matter what,
15:29I want you to be at least the size of our element, at least the height of our element.
15:34I don't need to trace this out anymore.
15:35I want you to be at least the height.
15:37You could be or whatever the margin is or the height, but I want the biggest value.
15:41Now, the biggest value is now there.
15:43And now we could actually add the padding that we want to add to it.
15:46So now I'm going to add the margin top and I'm going to say, hey, I want to add to it a padding
15:50and the padding could be really any value here.
15:53I really want it.
15:54What I really want to do is I want to take maybe a third of the padding because there's a lot of padding.
15:58So I'm going to go ahead and just divide my padding by three.
16:01And again, I'm just going to ask for it to, I could use the math dot round.
16:06I'm just going to round it down to make sure that it's going to be an integer
16:09and it's not going to be a floating value because we can't have floating pixels.
16:13So you see how we're using objects or using functions.
16:16And again, I just want to walk through the process again.
16:18First step was I got the raw value using jQuery and the CSS property.
16:23I just got whatever value was there.
16:25The next step was if there was any pixels inside,
16:29that means the value was already set.
16:31Then I'm going to go ahead and I'm going to basically fetch that value
16:35and only get the numbers out of it.
16:37Basically saying that I'm going to slice out from position zero
16:40all the way to the position where the first, the letters PX are,
16:44getting only the numbers and then converting it into a number.
16:47Next step, I'm saying, hey, I want you to be or the margin or the height of the element.
16:52Next, after that, I'm basically saying, I want you to add to yourself also a padding
16:58that will be the third of the basic padding that we had.
17:01And last but not least, I'm also tracing out.
17:03Now, the only problem I might have is if there was no pixels at all.
17:07So what I would like to do is I want to take all of this out of the if statement
17:10because it doesn't have to live inside of that if statement.
17:13The only critical thing to live in the if statement is that part with the pixels.
17:18Now, the next step is we want to make sure that basically if our margin top
17:22was not even defined here because if the margin top doesn't have any pixels in it,
17:27I just want to make sure that if not, I'm going to use a property that's called else
17:32of the if command, which basically says if this was true, if it had the pixels inside,
17:37if there was an index which was basically larger than one because zero would come
17:42back as false and then the if would not come back.
17:45So if it doesn't find the P, by the way, let's, before we even do that,
17:49what would happen and let me just put your console.log.
17:53What would happen if and I'm just going to type here just the cats are really cool
17:59and I'm going to basically just search for the index of dog.
18:06Now, there are no dogs inside of this.
18:08So let's see what would happen if we search for the index of dog,
18:12but there is no dog.
18:13So if I go ahead and I run my application just one more time,
18:16we'll see that we'll get the value of minus one if there is no dog
18:21inside of our element.
18:22So in other words, looking at our element, if there is no PX,
18:26we really want to make sure if it doesn't equal minus one
18:31or if the value is greater than minus one.
18:35We're going to talk more about that if commands a little bit later on.
18:38We're going to have a whole chapter dedicated to if,
18:40but really what we have here, we're basically saying, hey,
18:43if the value that comes back is not negative, that means it is,
18:46it does exist, then do all this great stuff.
18:49If not, is our L. So in every other case, do whatever is inside of here.
18:54And in this case, I want to say, hey, margin top,
18:57just set you to zero because we really don't have anything inside here.
19:01There's going to comment that out.
19:02So what I'm doing by this is I'm basically making sure
19:05that margin always has a number no matter what.
19:07If we could get a valid number, we put the valid number inside.
19:11If we can't get a valid number, we're just going to set it to zero.
19:14And now we know that we're out of that if statement,
19:16we could continue checking to see if our margin is larger
19:19than the height of our elements.
19:21And then we want to add the padding into it
19:23and make sure that it's rounded out.
19:25And we don't need this log anymore.
19:28And then all that's left for us to do is to animate it in.
19:32And all this happened only if our bottom item was there.
19:36So it's like this really big slab of code.
19:39So if I save this and we go back into our element,
19:41we should see our element now working really nicely.
19:44It's going to basically animate just a little bit out
19:46so it's not overlapping.
19:48And that's because we set there that third parameter.
19:50And if our element, just to really lock this up,
19:55if our margin top was still at 30, let's say, or even 50,
20:00it would add to that padding
20:02and it's going to respect the original padding that was there.
20:05So it's just going to push it down a little bit further.
20:07And that's it.
20:08And obviously, well, not obviously,
20:10but let me give you a little bit of homework.
20:11What if you want to make sure not only,
20:15not only that we're only going to add that max value,
20:19how about we also only add a max value for the padding?
20:23In other words, we want to make sure that there's a maximum size,
20:26a maximum size, and a minimum size.
20:28So that way that we don't add padding
20:30if an element is 50 pixels long.
20:32So that will be your homework.
20:33We're not going to even look into that.
20:34We'd love to see how you solve that problem
20:36in the comments section.
20:38And that's it about making things dynamic, really,
20:41because really, this really solves most of our problems.
20:44For example, in our margin top here,
20:46we could leverage the same type of logic
20:48where we take that padding divided by 3.
20:51If we want to even,
20:52we could go to the beginning of our element here
20:54and we could put here another variable such as small pad.
21:01And our small pad could equal basically
21:03that value that we created.
21:04So I'm just going to go there and just copy this, cut it out.
21:07And that will be our small pad.
21:10That way we don't have to do the calculation more than once.
21:13We could leverage that variable
21:15and basically assign that variable in all the places we want it to be.
21:18So small pad would work here
21:20and small pad would work here
21:21because we want to put that margin top there.
21:23We could just set that to be the same small pad.
21:26The last thing that's left for us is to add this delay.
21:29So I'm just going to go ahead
21:30and put here another property in our function
21:32if I find it up here.
21:34And I could put here another property such as delay
21:37and the delay will set it to a default to be 0.4.
21:41And I could literally just take this variable now
21:43and go ahead and just add that delay immediately right into its position.
21:49And I could find it also in the 0.8 area and just duplicate it by two.
21:53So there's like a progression in our delay as more elements are added.
21:57And that's it.
21:58Now everything here is dynamic.
21:59There is nothing here that is hard coded.
22:01And because nothing is hard coded,
22:03we could very easily use this expand three right to animate other items.
22:08It doesn't have to be these three items and four items that we're sending in.
22:12And we could very easily change our delay amount of time.
22:16Really the only property left in here that isn't dynamic is the actual amount of time
22:20it takes the elements to animate because we hard coded those values into the tween.
22:24But we could very easily, and that could be one of your assignments as well,
22:27is add that into the properties to make it dynamic as well.
22:31But we're not going to do that.
22:32We're just going to look at our animation and see that works
22:34if we send in a much bigger padding.
22:36And that much bigger padding means that our animation will be one after each other.
22:41Instead of one on top of each other, which isn't really the best of delays.
22:46So I would personally just stick to what we had or oopsie.
22:50What did I do there?
22:52Stick stick to what we had.
22:57And I'm just going to do this, maybe zero dot four,
23:01a little bit faster than our zero that three,
23:04a little bit faster than our default.
23:06Maybe a little bit faster would be nicer.
23:09Yeah, that works for me.
23:10All right. So that's it.
23:11In the next lecture, we are done working with associative arrays,
23:14which was our objects.
23:16We are done working with this really complicated structure.
23:19And although it's complicated, and you might think,
23:21but why do I want to create something so complicated?
23:23The answer is, is because once you create this, that's it.
23:26You don't have to walk into it ever again.
23:28You could use this in every single future project that you create.
23:31Now, it might look complicated, but once you create it,
23:34and once it works, that's it.
23:35You don't have to get into it ever again.
23:37And you could re leverage it,
23:38reuse it very easily because you didn't hard code anything inside.
23:42Now, in the next lecture, we're going to get back to that topic of arrays,
23:46which we wanted to talk about already previously.
23:48And we're going to see how could we leverage arrays to make,
23:51first of all, learn what they are and how they work,
23:53and then see how could we leverage them to make our code even more
23:56smart than what it was until now.
23:58So see you in the next lecture when we do that.
