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

Conditioning to conditions

Throughout this whole course we've touched conditions lightly here and there, mainly when working with Boolean values. This chapter is dedicated fully and completely to conditions. In this first lecture, we're going to revisit that if else condition and understand it without any conditions... ;) So, set your conditions aside and let's start working with conditions.

Video Transcript

00:01Duh-luh-luh-luh-luh-luh
00:05Dun dun dun
00:07Well hello, do you want to come over for dinner?
00:13Exactly you get it right so the course today is this whole
00:20Chapter is about
00:22Conditions and and forks in the road so let's meet we've already seen it throughout the course
00:27Let's meet a dart off with a very very simple page
00:31You can see here the
00:33Little logo, you know, we kept it really really clean chapter 7. So let's look at the source files for a second
00:39So literally chapter 7 I almost deleted everything all I did was added the text the line
00:44So our image will be on the center of it our main focus
00:47Oh, and I added also a few colors here a few variations of blue because our goal throughout this lecture is
00:54to discover the if condition and I thought what would be really nice if we created a
01:00Color transition based on the time of the day and we're gonna use if statements for that
01:06Alright, so let's let's get started. And the first thing I really want to do is I want to create a really cool animation
01:11Kind of like that Apple style
01:14You see it a lot in movies also where you have this white in and some modern websites have that it's a very clean type of
01:21Almost sexy transition into a website. So let me show you what I mean
01:25alright, so first of all what I'm gonna do is I'm gonna I'm gonna approach my tween max and
01:31What I want to do is I want to do an animation from and I want to animate my body
01:38And I'm gonna go ahead and I'm gonna tell to my body first
01:53I'm gonna say I want a two-second animation and I'm gonna go ahead to the property background color
01:58Because I want to change the background color to start from
02:04Total white
02:07Okay, and from that total white we're gonna transition into our element
02:11No, if I just go and just set our color to be totally white and I just run this you're gonna see that
02:16It's not gonna look all that because we have a lot of elements on our page
02:20So if I do that, we'll see that my page is white and only the bottom area is fading in which isn't really impressive
02:26I want everything to fade in
02:28Now I could literally go to every single element on my page and fade it in but that seems like a lot of work
02:33So how about if instead of that I go ahead and I set the alpha of my body to zero my opacity to zero
02:41Because once I set my opacity to zero, then I'll be able to actually
02:46See it basically I'll be able to get what I want. So I'm gonna go ahead
02:49I'm just gonna add one more property gonna set here. Maybe opacity
02:54I'll set that to zero. I'm gonna also add a small delay not small deployed delay
03:00And I'll really just a really really small like a hundred milliseconds
03:05let me say that that way when we
03:07Launch our page and we click on refresh
03:09We're gonna have white and we're gonna fade in so that way even if the site loads really quickly
03:14We're gonna have this really really nice fade in now
03:18by the way
03:18And you're right if I would go ahead and I would just come here and I would delete out my background color and just
03:24Animate the opacity I would probably get a very similar effect if not the exact same effect
03:30because our background is white so and
03:34Our we set our opacity and by default when we set the opacity for our body to zero
03:38Then we won't see anything. So really that background color wasn't really necessary
03:43But it doesn't become incredibly useful if we decide that we want to change our background color based on the time of the day
03:49Which is exactly where we're leading into our main topic
03:53So let's talk a little bit about if conditions and get that sorted out
03:58So we've met already if conditions and we said that we have the if command and put we put here the condition
04:04Or the rule so far we were working mainly with a boolean value value that we would have here
04:10That would be always set to or true or false
04:12And and then we would basically open a curly bracket close a curly bracket and do something only if that specific event happened
04:20so let's go ahead and
04:23Start creating something real
04:25So first of all, what I want to do is I want to get the hour of the day right now
04:28So I'm just gonna call it hour and what I'm gonna do is I'm gonna create a new date
04:32And I'm not even gonna save that information. I'm gonna go ahead directly because I want to get the hour
04:37now, let me just come let me just
04:40If true, let me just set your if true because if true means that it would always happen
04:44And I'm just gonna go inside here and do a console that log
04:47Hour see what hour of the day are we in right now?
04:51So if I go ahead back to my browser click on refresh
04:55First of all, we're getting undefined because that property doesn't exist. It's actually hours
05:00so let me just set it to hours and
05:05Refresh it one more time. And again, if you forget like I just did if you forget the documentation
05:09Just go ahead on the bottom here. Just type a new down date and just search for that property
05:15So I think it was you see so there's a method a function here. It's not just a property. It's get day
05:19So it's probably gonna be get our get hours or set hours get hours. There we go
05:25So you have your there's a get hours here somewhere get hours
05:29There we go. Get hours
05:33So if I set it to get hours, then I would see that value of 6 which is because the time is 6 in the morning
05:39So let me go back in there and I'm gonna use the property get hours and again again
05:44Don't worry about learning things by heart. I never learned them by heart. You shouldn't learn them by heart
05:49There's always an easy access to documentation. So it's not about learning by heart
05:53It's about learning how to leverage the skills that you have there
05:56So they just say this again if our if is true then we're gonna go into it no matter what so we should expect to
06:01Get that output of 6 in our and we sure am are getting that output of 6
06:07Alright, so back into the editor
06:09What I want to do now is set here at if condition that if our time is let's say until 7 in the morning
06:15So let's say that if our time is between well, not even between if our at time
06:20If our hour is smaller than 7
06:25Then we're gonna do we're gonna change that default color. Alright, so let's do this
06:29So first of all, I'm gonna set your color and it's gonna be our starting color and by default
06:36We had it there at white
06:38But really we want to what we weren't happening is if it's not 7 in the morning yet
06:42I'm gonna go I said here a few colors of blue from light to the darkest
06:45So I'm gonna go ahead and change it to a dark blue
06:48Which basically will kind of like hint that it's a nighttime. So let me just uh, there you go
06:55Let me just paste that value in there and notice what I'm doing here. I have your a default value
07:00That's on line 15 if our hour is less than 7 in the morning
07:04Then it's gonna go ahead and it's gonna change that starting color. And now that I've done that I could go ahead here and
07:42Just change that
07:44Color that I'm sending into the animation to be the starting color
07:47So if you go back in and we look at our application now instead of getting that white transition in we're gonna get a dark blue
07:54Transition in and you know what? Let me also put here another
07:58Another slight delay. So I'm gonna if I go into my editor here
08:03I'm gonna add here another even a little bit longer delay
08:07So let's see even zero to five seconds and if I go back into my document click on refresh them
08:13We're gonna start off with this dark color and we're gonna fade in and for me that fade in is way too fast
08:18So I'm just gonna tweak it a bit because I think the darker the color is the more time it needs to fade in
08:23But this is really just about you how it feels to you for me. This feels way better having it a longer animation time
08:31Alright, so that was if our time is before 7 in the morning, which it is
08:35So if our time is before 7 in the morning, we want to do this color
08:39What if we wanted O2 to set a default value, right? We have here this default value
08:45I could have easily instead of setting this default value this way say else and else would basically mean
08:51Else would hate that
08:56Here we go. I hate typing something that it was already typing before
09:00Alright, so I'm gonna 2 3 4 5 6 and by the way if you don't know and there's my there there's a chance
09:05You don't know what these numbers are
09:08These numbers are basically a hexed the mesel value and a fur color
09:12It's a it's three channels red green blue now the red
09:18And green and blue have 255 values, but because they're indexed not between 0 and 9
09:24But instead of being index from 0 to 9 which you're probably more familiar and more more comfortable with there's also more
09:32Characters after 9 it's a B C and so on and so forth until it gets to F
09:38basically, it has 15 digits instead of having only not only 10 digits making it possible for you to use two characters to
09:47describe a lot more
09:49variations
09:50So literally that's all it is. There's three color channels and here when I'm saying f f f f f f f
09:56I'm basically saying the full color full color 255 on all which together will create white
10:01If I wanted to create black, I would just do a black channel on the red
10:05There's no no red coming in no green no blue
10:09And then I would get black and all these color variations are colors that come through through our 1 2 3 4 5 6
10:17Our colors that are coming in that in that gamut now
10:20We don't have to memorize the color values because there's tools
10:24Such as color picker right here, which you downloaded already if you did all our Firefox downloads
10:29Which was I think Fox one?
10:32smart URL
10:34IT slash Fox one Fox two Fox three and I think it was a Fox four
10:38If you click on it, you could literally just hover over a color you like on any webpage and just grab that color
10:44It'll be copied into your clipboard and then you could go inside and then paste that color
10:49So you could work with it on in your site. So I already collected a few colors for us
10:53So let's continue so we started that if our if it's before seven
10:58Then we're gonna show that dark blue if it's after seven then we're gonna show a lighter blue
11:03Okay, so now because it's it's still six in the morning what I'm gonna do
11:07I'm gonna hard code a value here because I want us to experiment a little bit of things
11:12So that's true if it's four four in the morning or you know three in the morning or four or five
11:16I really wanted to be this color
11:18But in reality
11:20What I want to happen is if our time is between five in the morning to seven in the morning
11:26I really want to show them this color. It's a lighter blue
11:29so let's just see the right now again if if if I said the hour to five notice also because I said you're smaller than and
11:36I set the value to be five. It's automatically gonna go to the white area and now I'm gonna ask you why as I show
11:43it to you, so why why am I gonna see white and not the dark blue and
11:48The answer is is because I'm saying if our is smaller than five and if I'm saying our smaller five is not smaller than five
11:56If I wanted to include o to five in the morning, I would set our smaller or equal to five
12:01So really let's just look at the different values that we could put here
12:04We could set here smaller. We could set here bigger. We could set equals to we could set smaller or
12:11equal
12:13We could set larger or equal and there's one more which is equal equal equal, but we're not gonna talk about it at all
12:20Okay, so that's gonna be something that we might say for one of our more advanced courses
12:25But in general, it means an explicit equals, you know, why not? Let's talk about it for a second
12:30It's not a hundred percent connected, but it's a good thing to know
12:33So why tell you that I'm not gonna teach you something if it's easy enough to do
12:37So let's look at the console for a second if I set five equals equals five, that's true
12:43But if I if I ask five equals equals true three times that is true as well
12:47So you don't see a big difference here, right? But if I said the value of five as a string equals five
12:53I'm gonna get the value true. Now. The reason is is because when we're doing a comparison
12:59JavaScript and many other programming languages as well will automatically convert one of the values to the same type as the other one because this
13:06Is a string and this is a number you will automatically convert one of the two
13:11Convert the string into a number so you could compare the two the two together and it's true with a lot of different data types
13:17your
13:18Programming language in our case JavaScript is gonna try to convert it
13:22But on the other hand if I check if five equals equals equals five, I'm basically saying don't
13:29Don't convert is it exactly the same don't do any
13:33Adjustment to check if they're the same or not and in that case the value is false now
13:38We're not gonna see it in action of where could we use it in our applications currently because we just don't have a usage
13:44For it right now, but that's definitely something that you might need at some point
13:48Alright, so let's get back into our example because we weren't done yet now so far what we did and we're gonna continue this in the
13:56Next lecture, but so far what we did was we we set ourself. We got our hours. We set ourself our
14:02You know hour of the day check to see if it's that hour or less then we set a dark color
14:08Now every other case is white
14:10but in reality
14:11I want to create a few different cases and really what I want to use is something that's called if else and
14:17By the way, just before even we wrap this up if this condition is matched
14:21That means that this will never be executed with if conditions when they're chained together this way
14:27Only the first thing that comes back is true is what triggers so if that wasn't true, then we skip all this block of code and
14:35Trigger whatever is inside of this lock block of code in the next lesson what we're gonna
14:40Do is we're gonna add more conditions because maybe I want to if not only if it's smaller
14:45But also if it's between a certain range
14:47Well, how about if it's smaller than seven but larger than five and smaller than nine but larger than seven and so on and so forth
14:54And that's exactly what we're gonna see in the following lecture

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!