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

Phil Choi teaches a 40-year-old beginner JavaScript control flow through a fuel economy example, then moves into switch/case statements with a switch(true) pattern. The session includes exercises like mapping hours to days of the week before moving toward loops.
Chapters
You are camping with your friends far away from home. But when it's time to go back, you realize that your fuel is running out and the nearest pump is 50 miles away. You know that on average, your car runs on about 25 miles per gallon and there are only 2 gallons left. Consider these factors. Write a function that tells you if it is possible to get to the pump or not. Function should return true if it is possible and false if not. So I know we didn't get into functions yet.
Okay.
Let'S just pretend that this is a file. We don't need to do the function here. Let's just say constants. Const. Distance to pump equals 50. Const. Miles per gallon equals 25 gallons. Fuel left equals 2. 2. All right. And then we want to write something that says if they have enough gas to get to the. To the pump, return true, else return false. You want to take a crack at that?
Yeah. Yeah, let's go for it. So then I first gotta take into consideration the distance of the pump.
Okay.
If distance, the pump is what I want to say.
The sharks or the alligators, maybe. Yeah.
If it's greater than, greater than or equal to mpg times fuel left.
Amazing. Maybe.
This is the last math question I'm doing. If this is right, then again console log drive on, brother. But if not, we're going to call for help. I don't know if that's distance. Yeah.
Why don't you take a look at the code a little bit longer and try to figure it out a little bit. So if distance to the pump is bigger or greater than what you have left, you want to start driving there.
This one's pump is greater than. No, it should be less than. Right. Less than my. My total fuel.
Yeah.
This is 25.
Yeah.
Because I'm going to go three times 25. That's. That's what I want. And then I want to take that number and it's got to be larger than my distance to the pump.
Exactly.
Okay.
I just usually like to put a parentheses inside.
Okay.
Know that it's there. Like these are together. And then console log drive on, brother. Else call for help. Cool, cool, cool, cool. I think you understand if else now. So now you have. The tools you have in your belt are probably like console log logging, like assigning variables, operators like greater comparing plus now multiplication. And now you have if else. Switch case is very similar to if else. Switch case is a control statement. So like if else is a control statement. Case is a control statement. So it's just A control statement and control structure is just all of them put together. So it's just a set of instructions.
Okay.
It controls the script, right? So Switch case is a control statement that allows a value to be tested again against a list of values. It is similar to if else statements. If the case is this, run this one. If the case is this, run this one. And then default is our else. We have const. Fruit equals orange. All right? And then I put the fruit in here in the switch statement. And then in the case of. Okay, in the case of Apple, we're going to log fruit is Apple. All right? And then we're going to break out of here. And then case, if it's an orange console log, fruit is orange. And then if it doesn't hit any of those cases, then it's not a fruit. If fruit is not in case and is. And then we just put plus something like this, right?
Okay.
And then so say it's like Strawberry, it would go into default, right? Because it's. It's case is not an Apple, the case is not an orange, and default fruit is not in the case. So there is a hack to Switch Case that I want to teach you right now. So what I like to do in Switch Case, a lot of the time that a lot of other languages have, but JavaScript strangely doesn't. Price equals 1000. All right? And then we say switch on true. So when it's true, I want, I want it to work, right? So if price is under. Is under 1000, I want to. Console log price is under 1000. Case. Price is over 1000. Console log price is over 1000. And we just put a break statement here. Break. And after every case, when it break, you have to break it or else it's going to keep on running the code. Okay.
So it's like once it finds it, once it finds.
Yeah, or else it's going to. If you don't put the break inside, it's going to keep on. It's going to go into. It's going to run this code block, but then it's going to. It's going to check this case and then it's going to run into the default. So you have to break or return. So and then this is like the hack of Switch case that a lot of people use. And then case, and then we can just say default. And the only other one is console log price is 1000. So basically when we earlier, when we did our orange one, the case when it was orange, it was true, right? It was orange. Orange is matching orange in the case. Right. So now, now we're looking for switch, and then we're saying true. So now we're looking for basically this to equal true.
So in the first one with the orange and Apple work, we're just checking. But I guess that's what's throwing me off is the part that it's. If it's true, like, I guess that's what's the difference of you just putting switch price there. Like, isn't it the same thing?
But price is 1,000. So this, this expression doesn't equal 1,000. Right. Okay.
Okay, that makes sense.
Does that make sense?
Yeah, yeah, yeah. Because now I get it. Because, yeah, it's under 1,000. I get it in this very specific scenario. But I don't know, as far as outside of this one, I don't know. I'm not seeing what the difference is between the. Like, why, why you would put true or false there.
Like this. You want. You're putting expressions instead of the actual case. Instead of like actually saying, hey, match this string to this string, you're saying match true to this expression. Okay.
And then now it's just searching for what is actually true, true, but none of them.
Right. So it goes in a default. And that, that does kind of tie into the next exercise I have so we can do this one for homework assignment. It's. It's gonna be difficult. Yeah. I expect you to ask me questions. Okay, so it's return the. The day of the week based on how many hours. So let's say, you know, a week has 24 times 7 hours, and that equals 168 hours. If the hours are less than 24, return Sunday. If it's less than 48, return Monday. And we're just gonna go straight down. So like this one, I wanted to see like a switch true and then a case here. And then you, you'd have like const. Current hour equals like, let's just say 100 and then case. If current hour is less than 24, it would be Sunday. Right.
When you run this in the terminal, what does it look like? Like right now? If.
If right now?
Yeah, if you, if you ran it.
I think it would just. It wouldn't. It wouldn't catch a case. So it would not do anything. I'll show you. It would. It wouldn't catch a case here. So I will go through the switch case file with you. Okay, so make a variable and compare with the switch statement. So const. Whatever variable switch. And then this is case one Right. So why don't you try to do this real quick? Like const. Whatever. Like team equals Raiders. What do you want to do when the. When the case is Raiders?
This is case one.
Maybe you want to console log, like, Raiders are the best NFL.
Yes.
Comparing this string and then it's saying, hey, switch on this variable. And then once you can match this variable to this, like, whatever case it is, that one. Okay, so maybe case Broncos. Here we'll add another case, and then console log, Broncos got whooped by the Raiders. All right, and then we'll break. So team is Raiders. Right. So it's going to go into this case. Right?
Right.
Won't even go into Broncos right now. So earlier I did say you can switch on true. Right. So if in here I put team equals Broncos, team equals Raiders, what do you expect to happen here?
It's gonna go to that first line, check Broncos, and be like, hey, it doesn't equal Broncos. So it's gonna go to the next one and read Raiders and be like, hey, that's our. That's what we want because it's true. And now we're gonna console log that, hey, they're the best football team.
Okay, cool, cool. So you. You understand, like, this expression is true, right? JavaScript knows it as true, Right. This expression is true. Right. So it's trying to match this true, and it's using guy. So that's the kind of like, switch case act that a lot of people use.
Okay.
All right, well, make a variable and compare it with the switch statement that has a default case. So can you just fill this out real quick?
Make a variable and compare with the switch statement that has a. That has a default case.
So default case is like the else that we had. So remember in the strawberry, this is not in the case. The fruit is strawberry Magic. It's not ball. Do you remember?
Yeah.
Did he retire already?
He should be. He has no ankles because his dad bought him crappy shoes.
He. They made crappy shoes? Yeah. Just to, like, bring it back home, I think, like, Lakers player is magic. Okay. And then here I'll put Lakers did not catch in case and player is. And then I'll just put, like, later, like this.
I see.
And then you could add more cases here. Like case.
And that way I can keep adding, like, the different whoever I want to.
Exactly. And in the end, you'd exit. You'd know exactly. Hey, I didn't. I didn't account for this case, you know?
I see. So it's like it runs through it finds it. And then my default is at least because we put in that console log, it's going to say what it actually was.
Yeah, yeah.
Oh, shit. It was Lebron the whole time.
Yeah. And then you can like write your code a little better or whatever. And then let's just make a final one. I'm not going to give you a boilerplate code, so just make a variable and compare it with a switch state. Switch statement that has multiple cases.
Oh, geez, we're going with more games.
All right, so. So didn't find your current game? Current game it is Mario. Okay, so here, like, let's say, let favorite game equals undefined. So we don't know what it is yet. And then if the case. If the game is here, we can say favorite game equals Ultima Online. Favorite game is undertale. And then we can just say favorite game equals game. And then in the end console log, favorite game equals favorite game. And then that's kind of like a way that you can use it. Because in like an if else or a Switch case, you have something that needs to run, right? And maybe this guy's favorite game and maybe there's like five people that you need to. You need to run this code with. This is kind of a cool way that you can use it, in my opinion. And actually, to be honest, we could just change this to game every time, but for the sake of it, do it this way. Ultima Online. All right, so now favorite game would be Ultima Online. Cool. Right? There it is. Any questions so far?
Not. Not yet. Not yet. I'm sure I'll come up with someone. I'm gonna start doing that. That work.
Okay, so Switch case is very similar to if else, Right? Think about it. So there is this thing that I want you to do over, you know, tomorrow. And then let's just go over the for loop and while loop really quick.
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






