Transcript
00:00 Great. Let's do this. So we're gonna start by creating a variable called grade. So we're gonna make that a let because we're gonna change it. We'll set it explicitly to a string.
00:11 Because we're not setting it to anything, now TypeScript doesn't know what this is so it gives it what's called an any type, which is basically just anything, and it's not very helpful from a types perspective. So we're gonna specifically say this is a string. We're not gonna assign it to anything though, so it is not it doesn't actually have a value until we've assigned it to something. And now, we've got our score right here. We're going to say if the score is greater than or equal to 90, and we hopefully you remember some of these comparators that we did, these comparisons right here, then, yeah, then it's an a.
00:49 Otherwise, b, c, d. Or if it's anything lower than 60, then, yeah, you're gonna get an f. My I I was gonna say my apologies, but, like, I didn't do anything wrong. It was your grade, man. So I do better next time.
01:07 Alright. And, then we're going to make a past. I'm not going to assign this to Boolean. I'm gonna try to ease you into learning about inference and things. So we're not actually going to assign this a specific type because it's able to infer that based off of the logic that we've got or the expression that we have on the right side of that equal sign.
01:30 So if the grade is not a d and it's not an f, then we know that we passed and we can console log all of that. Whoops. Console log. There we go. And we can export, save that, and here we go.
01:41 Score was an 85. That's a b. We passed. Hooray. If you want to, as just some extra credit, you could go through here and do b plus and b minus and and, all of that.
01:52 So feel free to to play around with that. But, yeah. This is the, the the basics of using if and else. This is our, comparison right here. This is what determines whether we're going to fall into, this if this if block right here.
02:12 This is called the consequent. So what happens in the case that this expression evaluates to true? And then, you can have an else if. So you can have an if all by itself and that's it. And if we do that, then we're gonna get our grade is undefined because we're not handling, the case of less than 90.
02:31 You can do actually, we could redo this whole thing without, whoops. There we go. Without these else, right here. And ultimately, that would, end up being, yeah, that would not end up being the same because we're saying, hey, if the score is greater than or equal to 60, then give me a d. And so what's happening here is we're saying if the score is greater than or equal to 90, that's an a.
02:56 Ours is not, so we're gonna pass through that. Then if it's greater than or equal to 80, then the grade is a b and then, as a totally separate statement, if it's greater than 70, that's a c. Okay. Well, now it gets changed to c. And then as there's another totally separate statement, if it's greater than or equal to 60, then that's a d.
03:14 So that else part of the the the clause there, that, special syntax, that keyword is actually important for, tying those if and else statements together. So if this, then basically the what the else is saying oops. Else. The else right here is saying, if it's not that but it's this, then continue. But if we leave that off, then we're just saying, if it's this and, yeah, of course, it it's, greater than 80, it's also greater than 70 and so that's where we end up in that situation.
03:49 You could rewrite this a little bit to make, this format work without the else statements. So you could say, if score is, oh, yeah. Here we go. Is greater than or equal to 80 and the score is less than 90, then that would work. And we do that all on the all of these.
04:06 Then, we're going to get the same result here. But between you and me, I think let's just use the else if statements because I think, it's a lot easier to understand how all of that works. So, you wanna think about how the code flows. So you basically, you put a value for the, relevant variables in your mind and you kinda follow the code flow and that will make things a little bit more straightforward. Alright.
04:32 I think that's everything that you need to know there. So I hope you enjoyed that one and we'll see you in the next one.
