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

Playing with cats, functions and objects

This lesson is not about learning new things it's about practicing them. Although we might learn a couple of new small tricks, the main focus will be rehearsal. I encourage you to take a break and play around with objects. Oh, we will talk briefly about constants.

Video Transcript

00:00just like, say hello to them, just like well what am I talking about I think
00:06holding a cat is confusing. Well so far we learned about objects and we learned
00:13a lot about a lot of stuff we're not learning anything new well we are
00:18because we're practicing and seeing it how to use it in action right go down
00:22so by the end of this lecture we're gonna have a running kind of like not
00:27o'clock but something that will give us how many hours past since I was born how
00:31many days past since I was born the real concept the real idea is to practice
00:35functions and variables and objects because really programming is all about
00:39practice and when you finish this course if you just walk away you're not gonna
00:43learn a lot your goal is to sit and create things the more things you create
00:47with the skills that you have you don't have to search for more things to learn
00:51really online you just have to practice the more you practice the
00:54more you'll know obviously we have a lot of other courses which I think are great
00:58and you'll love but the biggest point is practice and if you practice and you
01:04integrate what you're learning if you start communicating with others if you
01:08comment in the comment box if you show us your homework you're gonna do really
01:11well like a lot of our students that are doing fantastic and amazing stuff
01:16all right let's get back so I'm gonna go back into the editor and what I want
01:21to do next is we created the get seconds from birthday which is right here
01:26well what I want to do is I want to create a few more helper functions and by
01:30the way as I'm doing this we're learning some new things but not really
01:34we said that functions it doesn't matter their order we could create them
01:37anywhere at any point contrary to variables that we can only use them
01:41after we define them so I'm gonna go ahead here all the way in the top and
02:05I'm just gonna create a function called convert to minutes convert to minutes it's
02:15gonna return back the amount of minutes and literally what we want to have here
02:18is we want to send seconds inside and we want to return minutes so let's first of
02:25all create that variable and call so I'm gonna call it min for minutes and I'm
02:32gonna call the convert to minutes and send inside of it our seconds and it's
02:39very very similar to the process that we had when we were working with the day
02:43but now we're getting the raw seconds already because we got it back from our
02:46get seconds from birthday and we want to convert them into minutes and there's a
02:51big question do we want to have it with their leftover do we want to have it
02:55without the leftover in our case we want to not have the leftover so we're
03:00just gonna assume that we're working without the leftover so I'm gonna go
03:03ahead and I'm gonna create that minute second and I'm gonna set its value to be
03:08the amount of seconds divided by 60 seconds now all that's left for me to do
03:13is also to round it but it contrary to rounding up I want to make sure that I'm
03:18always rounding down so if I want to always round down I'm gonna use the
03:23math object and use the function bottom or actually floor my mistake floor and
03:32you don't have to memorize it again it's one of those things that if you have the
03:35the you know a website that has that information in front of you it makes
03:40life easy or if you're working in with an editor that gives those hints
03:44automatically or for example if you're working in your in this case when you if
03:49you go into firebug and just type math it's gonna showcase to you all the
03:53different things and then you'll see that floor function but really that your
03:56best solution is having some sort of a reference nearby to make your life
04:00easier now in our editor which is the sublime text 2 you'll see that if I
04:06type your math dot and I start typing F for floor we'll start seeing that the
04:10hints come in the reason why I didn't get any hints was because I already had
04:13this value already set here and then I started writing floor so I didn't see
04:17any hints as I was doing it all that's left for me to do is to return that
04:22variable and by the way let me ask you a question and how about you answer that
04:27question in our comment section what type of variable is this is it an
04:32object is it a string is it a number is it a boolean value let it let me know so
04:37let me know what value is that variable what does this function return
04:41all right so I want to trace out the value that we got back from that min so
04:46I'm gonna just type your console log and I'm just gonna send into it our minutes
04:51to see how many minutes past since I was born so like go right into my browser
04:56again and we click on refresh we'll see that this is the amount of minutes
05:00that past since I was born beautiful so let's continue what I want to do is I
05:05want to go to my paragraph and I want to change the copy in my paragraph so
05:10I'm gonna go ahead and do the same thing that we've done with when we were
05:14referencing if I'll find it up here as we were referencing at our item using the
05:18ID we could do the exact same thing only instead of that reference to the
05:22actual class because we only have one of those items on the page it doesn't
05:27really matter obviously if we had it twice and we would be putting that
05:30content twice but I'll have to put it here as dot and then the name of the
05:34class that I want to refer to next I could literally just type in my text and
05:49send into it whatever text I want it to be and I could go ahead and just add
05:53the value right inside and set my text that way so I don't I don't need that
05:58console anymore and if I go back into my browser and I click on refresh we're
06:02gonna see that now that that value is gonna be right in there exactly where we
06:06would hope for it to be so what I want to do next is I want to create a string
06:10value variable we're just gonna call it output or out for output so you know
06:16like s out just so I'll keep keep with the standards and I'm gonna type here
06:26minutes from your birth and what I want to do is I'm gonna just instead of having
06:33that min variable I'm just gonna go ahead and copy this value and just put
06:38that value in just right in its spot literally what I'm doing right here is
06:43I'm fetching the value that comes back from this function and then
06:47concatenating it with this copy oopsie why am I what am I doing there we go and
07:01let me just put here a semicolon and by the way if things are starting to get
07:06unruly you could always break a line to make it easier for you to see it all in
07:09the same place so now we don't need these two we don't need this anymore and
07:14we could comment this out in the meantime until we get our content ready
07:17so we have our copy ready theoretically so let's put our s out send it into our
07:22min sorry send it into our text of our object of our paragraph if I go
07:27ahead and click on refresh now we'll see the amount of minutes from your birth
07:32and we could go ahead and continue this mindset I want to really quickly create a
07:36converter that will also take us from minutes how about it will convert us
07:40down into days we're not gonna even do hours so we're gonna be function convert
07:46to days so convert to days it's gonna take in the amount of seconds that have
07:52passed and we're gonna convert that into into days so one thing that I could do
08:00is I could leverage that convert to minutes but what I want to do is I'm
08:03just gonna create a variable that defines how much a day is and I'm gonna
08:08set it as an uppercase because in for my case this is really something static
08:13I'm never gonna change it once I create it so it's a great way to say
08:36like once you define this this will never change so I'm setting it as a
08:39uppercase to make sure that I don't by mistake override it or add or change any
08:43content to it once it's created so how many seconds are there in a day so
08:48literally obviously we have 60 seconds in a minute and then we time that by 60
08:52minutes then we time that by 24 hours that will be the amount of seconds we
08:57have in a day all that's left for us to do is to return the value return the
09:04value of our amount of seconds oopsie divide by day now this is going to give
09:10us a leftover so we also want to set it rounded up and floor it actually to
09:14make sure that we get rid of whatever is there in the leftover now notice the
09:19small difference that I have here here instead of creating another variable I
09:23literally just went ahead and sent the value of my variable without giving it
09:27a name which is definitely something that I could do so this is a much
09:30shorter way of doing the exact same kind of mythology of what we've done before
09:35and now that we have the convert to days we could literally go ahead and maybe
09:39add here another plus and we could break a line by adding the HTML tag of
09:45break line and we could add another another line of copy right here which will
09:51be basically our convert to days and I could send inside of my convert to days
09:56the amount of seconds that I already have stored from the variable before and I
10:00could add here a little bit more copy and by the way I made a mistake here
10:03because I put plus twice so let me just make sure I only put plus ones and I
10:08could put here some copy that would say days since you were born since birth
10:16let's do this since birth I'll just make sure they have a space there so
10:25we'll be spaced between our amount of days that came through and the day of
10:30birth so that's it it's time for us to save it and test out our application
10:34and if I test this out one more time we could see that first of all I broke a
10:39line which doesn't really work because I was sending it inside as part of the
10:42HTML so I'm probably not gonna put there a break a line break I might put here
10:47just a little bit of extra spaces here because I'm inside of a paragraph and
10:51I'm sending this as a string as text inside of my element and I'm not
10:55sending it as HTML if I would on the other hand use that inner HTML property
11:01it would probably work differently so again if I come inside of here and I go
11:05to that element by just let's say I'm just gonna take that my book slide and
11:12just go ahead and just set that in that side of so we could see it in our
11:15console we'll see that when we cut get our elements back we also have the
11:19property of HTML of HTML now in our HTML property we're not gonna put here
11:25just the value of let's say we're not gonna we're not gonna just put the
11:33value of br because that wouldn't do anything but if we put here the value of
11:36HTML and I said here the property of Narf and I close it down you'll see the
11:43copy was changing because it's a function and I could literally just put
11:46here a break line in another Narf and then it would it would accept our HTML
11:50because we're not working directly with text so if we wanted to fix this issue
11:54and still keep that break line by leaving that break line inside we're
11:59gonna have to use a different function and not the text function but instead
12:02of the text function we'd want to use the HTML one so I'm gonna go ahead there
12:06and type HTML and that will make sure that it could add actually HTML into that
12:11element so if I click here refresh again we'll see now I'll have the
12:14amount of minutes and we'll have both to the amount of days since I was born
12:17alright so I'm giving you a little bit of homework in the next lesson next
12:21video we're going to continue doing one last thing to this so your homework
12:26right now is I want you to add not only how many minutes but also how
12:28many hours have passed since our birth and in the next lesson that's gonna be
12:34your homework would love to see that in the comment section I'll only comment
12:37about that and talk about that too for whoever that checks the comment section
12:41and does that but um in the next video what I want to do is I want to talk
12:44about how do we get this to update because you know a little bit of time
12:48passed since I was born you know like now another 10 seconds have passed and
12:51or more and there haven't they haven't been accounted for so in the
12:55next lesson we're gonna see and that's gonna be our first real animation and
12:58text directly without using any library so that's what we're gonna be doing in
13:02our next lesson

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!