Teaching my 40 year old student with no experience programming | Conditions

Phil Choi teaches a 40-year-old beginner how conditionals work in JavaScript. He walks through if, else, and else if statements with examples like checking if a number is greater than 5, then builds a longer nested example where a bartender checks age and designated driver status. The student debugs code live and gets homework to write the same logic two different ways.
Chapters
I took a lot off of this because it was just, it was way too hot.
And you knew this, this day was coming. You know, you gotta look good, gotta look good. So the next one I want to get into is if Else. So if you follow me into the if else folder into readme. If else statements in JavaScript are used to make decisions in code. They commonly use comparison operators to determine if a condition is true or false. So if this, do this. If not, do this. And that's basically all computers are right. There are zeros and ones, right? Oh, if they're older than 18, they're allowed to vote. If not, they're not allowed to vote. And it just makes these kind of decisions and you just make a whole bunch of them and then basically you write a bunch of instructions for the computer and it can end with the result you want. So if else statements are used to execute a block of code. If a condition is true, if the condition is false, another block of code can be executed just like what I said earlier. So the index is if, and then in parentheses, a condition do this, else do this. And that's basically all it is.
Okay, example.
Let x equal 10. If x is greater than 5, console log x is greater than 5. Elsewhere, x is less than or equal to 5. There's an if else and then there's an else if. So basically that's just adding another condition in the middle. So else if statements are used to specify a new condition if the first condition is false. So if there's a condition called else if, run this and then for all others, run this code.
Okay?
And then the next is ternary operator. It's just a fancy way to use if else. I don't think we need to get into it now. It'll just be a little bit more extra syntax and make your code look a little bit cleaner. Like, as you can see, like if else else. If it could, it takes up like nine lines of code or something, but turn it payments, you can do it all in one line. And I'll teach you more about that like later on in this lesson. So maybe we'll make a little comparison, like constant is full moon and then we can make a boolean, right? Then if we say if is full moon, we can kind of say console log werewolves are outside. And then else, everyone sleeping peacefully, just simply like this. Let's make an if else statement on under number one.
Yeah, const. I'm trying to think of what would.
Be easy one, let's do drinking at a bar or something.
Like if drinking at bar equals.
Or maybe like a drinking age or up to you. Up to you.
True. So I'm going to go if have another and then else. So then I want to come down to my next squiggly line. Else another one. Oh my goodness.
Have another as well. Yeah. Okay, let's do a little bit more of a complicated one. Why don't we do something like. Why don't we do const. Drinking age? Something like drinking age equals 21. All right, and then let's do const. My age equals. What is my age? 35. If my age is. Is over equal to 21, then you.
Can have a drink, right? Have a drink. Gotta leave.
Gotta leave. So like let's say 18. What do you expect this to be, right right now?
So now it should be the gotta leave, right? Because we're under. Under 21, right?
Let's make another one where it says already drunk. And maybe that's already. That's true, right? So maybe I am 35. But the next question I want to ask is if already drunk, gotta leave, else have a drink. Okay, you see how it can get kind of like deeper and deeper.
We have an if else statement with another if else in between it inside of it.
So now that if my age is over 35 or over 21, right. Then all we have to think about is this block of code now, right? Because it's not going to anymore. Okay, so now we're here, and then now we're asking, hey, is he already drunk? You know, he's too sloppy. All right, then you can drink, right? And then say he's already drunk. But let's say, okay, so he's not drunk. So let's say he's false. But make something is late, all right? And then let's say is true. So in here, another if if is.
Late and he's got a chug, like, hurry up and drink it.
Okay, Chug beer. Else we can just have a drink, right? So this can get deeper and deeper and deeper. You see how like kind of like if else can kind of control your code to do what you want. Okay, so now we are.
So it's kinda like we're taking the. We're taking the steps down or steps into it because we're still meeting that first. Like that outside one. Like, like you said, we're going, hey, we're 35. We passed that. So now we're in the if drunk or if already drunk, if else statement. And we're not drunk. So then we got to Go to else. And then now we're in that new bracket of okay, am I late or am I not? And it keeps like, kind of.
Right, right. Like going in and going in and going in a little. All right, so do you understand all this code here?
I think so, yeah.
Okay, can you. Can you like, kind of pretend you're a bartender and you have. You have these three variables to work with, right? This for homework, actually, is it okay?
Yeah, yeah, I'll do it for. I'll do it for homework. That way it'll be me and whoever's watching. We can make it happen.
Act like a bartender and make if else statements to see if customer can drink or should go home. Okay.
All right.
So else if we'll just go. We'll go over else if quick, and then we'll end the module. We have like six minutes left.
Okay.
All right. So else if. So else if statement is used to specify a new condition beneath the original if and above the else statement. It is used when there are more than two conditions. So. So you can make a. So it. It looks like this if something, do this, else if. So say there's like two ifs, right? Do this and then else for everything else, do this. Right. So then let's say something like if. What is something like this? Age equals 35. If age is over 20, can come into bar. Else if age is over 21, console log and have a drink else. All right, so which. Which outcome do you think will happen at this point?
So my if. Hey, age. My 35 is greater than 20. So I can go into the bar.
Exactly.
That's what's going to stop there. So exactly.
So it stops here, right?
Yeah.
It wouldn't hit this code or it wouldn't hit this code because it hit the first if. Okay, amazing. So you'd actually want to put this 21 up here and 20 here to make the code run how you want. Right. Say they're 21. Right. So where do you think it would fall here?
If age is greater than 21, but it's equal. So then it would go else if. If ages, it'll go down to the else if line where I now 21. So it'll say can come into bar.
Exactly. Cool.
Cool.
So that's kind of how the if else else if works. Seems like you got that in your head very easily. To make your code as clean as possible, you don't want to have that many indents inside. You want to keep, like, the more deep it goes to the right or whatever. And tabs inside your code starts to get harder and harder to read my opinion. You want to just kind of, like, where you can use else if and like, define conditions should because it makes a lot more readable later on. Okay. Or else you're going to get into, like, this thing called, like, indent hell or whatever, where you, you know, you're. If this. Okay, okay, if this, then if this, okay, if this. But then you can kind of keep everything kind of aligned to the left if you kind of define your conditions. Okay, all right. You know how you're gonna act like a bartender with if else statements? Let's try to do the same thing with else if on your own time.
Okay.
With else if as well. And those will be the two. Two tasks. And you can kind of message me if you have any questions, and if you get through them too quickly, we can always make it harder.
Okay, all right, then. All right, so I'll go. I'll work on those ones, and then I'll compare with. If anybody else wants to compare notes. We'll go from there. How I started out with is just at first, I was just doing, like, how we did it through the lesson, like, age and etc. That's how I end up with those. Those few arrays. And then I was like, oh, let me do an object, an array of objects that we talked about so many times. And so then that's when I end up doing all of these objects and then ultimately putting them in the array that is was supposed to be patrons. And then that's another question for you. Like, I was like, I knew I spelled it wrong, but I just kept going. And then now I was like, I should probably return sometimes and actually spell it correctly.
If you're ever in this issue, right? And go to the first instance of Patreons and hit Command D. Command D, Command D, Command D until you get all of them. And then your cursor will be there. And then you could just, like, change it if you want.
Okay. All right.
Okay. So let me just read here. So you're saying. Okay, does this work? It does. Do we need these guys?
Nope.
No. This is just seeing if it works or not, right? Yeah.
Yeah, that was me. Okay.
And then you have. You have three ages, 37, 40, and 18, and you have two genders, male and female. Oh, one second. Let me turn this guy off to disable. All right. D driver. So is designated driver true or false? All right, so then you have Mama Bear. Name Mama Bear. And then if you already made that age Array. I probably, me, personally, I would use it. You know what I mean?
Yeah, I. I slowly started building on it, and then I just left it there. Like, oh, should I come back to it and actually use it or just remove it? I was kind of.
Yeah.
Like, thinking if I was making a list of 50 people versus only the three, like, should I be using an array for that or. Or not?
Like, I. Like I. This is good practice, but I just keep the objects very separate to each other.
Okay.
I wouldn't use the array. Like, yeah, I would. Like, if there's only a one to one, that makes sense. Like, if, like, address. They can have, like, more than one address, right? Yeah, but if it's just one to one, I just put it into that object.
Okay.
Yeah, so. But it just makes your code a little bit more like, there. It adds complexity that it doesn't need to have. Okay, so let's just change these guys back to 37.
And then so I can just comment out the constant or.
Okay. Are you using it anywhere?
No.
All right. Okay. So then you have Mama Bear, Daddy Bear, Baby Bear. And then you have patrons. Mama Bear, Daddy Bear, Baby Bear. All right, so you can you pick off the property? And patrons zero should be Mama Bear. Cool. So that's what we got. I think we should see that log. Yes, this is Mama Bear. All right, and then you have scenario. So patron zero. So that's Mama Bear, right? Yeah. Enters the bar. So that's what we have. Mama enters the bar. Bartender checks ID if patron's age is over 21. All right, what can I get you to drink? So checks id. All right, what can I get you to drink? And then we didn't go into this else. And then we should probably keep these. If I would put, like, a space here, just to be honest. Your code a little bit more readable. So this is like a block of code, right? This if else. And then the block of code. Bartender checks id. All right, what can I get you to drink? And then patrons two is over.
It should be under 21, like, because he's 18.
Patron number. Yes. So if he's over 21, come in. Else if patrons one H. So that's Daddy Bear.
Yeah.
So dad will drive home. So that's working.
Okay.
And then patron one. Mama Bear enters the bar. Bartender checks the id. All right, what can I get you to drink? And then what were you having a problem with?
So this very last one, the round three, I think if you look, that last L statement, it's running, and I don't think it should be 79. 79 and 89.
And then the. The. His drink is Appletini, Please make it a double.
Yeah.
And then you want it to stop after this, right?
Yeah, like, it shouldn't go to the. I thought it wouldn't go to the else.
This is a separate block of code. And this is a separate block of code, right?
Oh, okay.
So it's going to run this every time. So if you do not want this to run, then I would put it inside of here or something. And then. And then actually, a way that you can just make it stop is just return each of these ifs. Like, when you make an if statement, first it's another block of code you're adding. So, like, if true, this is a block of code. If false, this is a block of code. If 5 equals 4, 3, this is a block of code. So each of these are. This is three blocks of code, right? Yeah. So, okay, so what I would.
What. I guess what I was going for was I was trying to do, if, like you said is true, then do do this. But then again, something inside of that, like, I thought we could do multiple ones. Multiple, like, ifs.
So I think this is the way I do it. Okay, okay, okay, okay. I got. I got what you're saying. Okay. So I'm just going to rewrite some of this code.
Okay.
One, let's just put it like greeting, right? So we'll just say, console, log patrons, one name enters the bar. All right, now this person, welcome back. This patrons, one name. And that part's good. Now, if. So the first one that we want to check is the age, right?
Right.
So patrons age, if that is greater than or equal to 20. And then here we say, like, oh, it is okay to drink. Right? And then if that's an else. And then we say this thing. Oh, it looks like you're a little young. Right? And then our next check here. If. If it's okay, right? We say, hey, are you the designated driver? Right? Say, hey, if patron, one dot if he's the D D driver. Yeah, it's true. Then, no, thanks. I'm driving tonight. This is what's happening, right? So and then if he's not, then it's. Then it's like, okay, all right, man, give me that drink and make it a double.
Right?
So maybe this is the way that code. This would be a little bit more simple because you have the. So this is how I like to think about it. You have the first really important question. Hey, are you old Enough, you know, and then you kind of have the if and then you make the L statement because you, you kind of split up the things, right? Like. Yeah, here, and then you maybe put an else here. Right. And then like, the subject matter is age. So you want to make that one block of code, and then the subject matter of the next one is designated driver. Right.
Okay.
And you want to put those together. So the first thing is in. In my mind, hey, are you old enough to drink? Are you? Then what are the actions that I'm going to have after I know that you're old enough to drink? Are you designated driver? Okay, then. And then if not, else, then make me drink. And then that block of code is in the if statement. And then else. We don't even need to run this designated driver code if he's not old enough. Right, right. And yeah, as the computer, you don't want to make it do stuff that it doesn't want to do or doesn't need to do.
Okay. So when I. So when I did mine, I just made three separate blocks of code, and that's why it was running all three versus what. What. What you just made, which is it has it within it.
Yeah.
Okay. Yeah, that makes sense.
This makes sense. Yeah. This is why I like to think about it anyways. Like, just there's a big question. Like, that's like the big if else. And then there, there could be other conditions. And then if it doesn't even reach that big condition, I don't even want to run the stuff in the what in the if yet, you know?
Yeah. Yeah. Okay, cool.
Did I answer every question?
Yeah, yeah, that. That answered it. That answered it. So now, now I'll try and take another shot at it again with that. I don't know, do you call it like an embedded if statement inside of another if else or like, I don't even know.
Just, you know. Okay, just double if else inside of the if else.
Yeah, okay, that works.
More from Phil

day in the life of a software engineer | 5 hour productivity | building my dev agency 🔥

day in the life of a software engineer | 3 hour productivity | coding my 6-figure community startup

day in the life of a software engineer | 5 hour productivity | coding our sns startup app LFG

day in the life of a software engineer | 5 hour productivity | creating apps that makes big money






