JavaScript Basics pt. 15 - More Comparison Operators

JavaScript
Transcript

English (Auto-generated)

Welcome back everyone to Javascript basics. We are here with part 13 and as I promised we're going to talk about some more comparison operators today. So let's dive in. So we've used the greater than and less than operators in the past when we've when we learned about if if else statements but they may be missing something. And I don't know if you've noticed this. You probably haven't. But let's let's go see what they're missing. So let's head over to r reflect here. Going to make sure that you have that already open. All right. So let's say let's write an if statement inside a function here. We'll call it comparing our We'll need more function five. We'll pass it a parameter called number. Here we go. And then we'll write our if statement. So let's say if the number is greater than five, we'll have a print a message to the console council dot blog more than five and then turn here. But if not when we return this console log, that's 10 5. So thanks. Call our function down here and pass a number. So let's see passive three And so three is less than five. That's true. Let's pass at six. six is more than five. So that is also true. But what happens if we pass it five itself. Thank you. Mhm. It says come it says it's less than five but that's not true because five is five. So that is what I mentioned or that is what I meant when I said that greater than less than operators may be missing something and that is where the greater than or equals to or less than or equal to signs which looks like this. So greater than sign with an equal or less than sign with an equal come into play. Alright. And before we head on over to replicate like the regular quality operator, this comparison will convert data types to check. Let's hop over to replicate again and now let's do equal. So now it's greater than or equal to. So let's change this message now. So let's say this number Is at least five. Alright, now let's hit run because we're passing it five still and whenever it's done running But you tell us this number is at least five. At least five. I don't know what it's doing, it's doing both because that is not the case Number is greater than or equal to five. Oh, I think that's because let's do Yeah, I believe it. So, so this number is at least five and then if we pass and a prototype, So let's pass it five as a string. And again this number is at least five. Alright, let's head back over to here. We are now moving on, we have the logical and operator. Yeah. So what happens if you have more than one condition you want to check at once, For example, what if you only want to send a message to easier if they're logged in and they have opted in for notifications when the logical and operator comes in handy. If you want to check more than two conditions at once. So let's go ahead and try that out. Hop back over to replicate it and we will erase this, clear out our console and let's write a new function. So we'll call this function compare and we'll pass the parameter of X. Then let's do if statement here. So let's say if X is, let's say if it's greater than 10 and Text is less than five, let's do okay. Actually, no, we're here, let's say time. So mhm No, we'll call it good afternoon. So let's name this, let's change this parameter to time. We'll call it time. So if time is and we're gonna go based on a 24 hour day. So if time is greater than actually, let's say less than 18 as an 1800. So six o'clock and time is greater than let's say 1200 as in 12 mm hmm. 12 PM. Let's say we'll return to console log of good afternoon because then it's afternoon. Between those times. If not, we'll do return council dot blog. Yeah, it's not afternoon. And so let's call our function down here good afternoon and we'll pass at the time. Let's say the time is uh it's 14, 1400 AK two PM. That's it run And there you go, it tells us it's good afternoon because it is the afternoon peasant, 19 as a 1900,007 pm it's gonna say it's not afternoon because it's not. So there we are. So the logical and operator is going to be useful if you have two conditions. If at least two conditions you want to check except we have the logical or operator. So what if you have two conditions and you want the function to return true if either of them are true. So you're not being very specific. So say like if it's afternoon or evening, tell them. Yeah. Hey, what's up? That's fine. So this is where the logical or operator comes in which is represented here by two pipes. And before I started learning how to code, I did not know the pipe key existed. I'd never used before. So for you, it will likely be between your backspace and return or enter key on the keyboard. Uh, it will share the key with the backslash. Alright, so let's go ahead and do an example with the logical or operator. Let's do a new function here. So go ahead and clear up the code right there. And then clergy council. So function, we'll call it, uh, is moving good. Um hmm let's see here. Mm hmm. To function out and then we will test a parameter called score. So we'll write an if statement and we'll say if score is greater than, Let's say if score is greater than 60 will kind of go based off like the rotten tomatoes score where 60% or higher is fresh. So if score is greater than 50 kevin or and we'll write her two pipes, their scores greater than 50 or um let's say Scorched greater than 15 or mm hmm. Or Yes, do data type of Yes, people console dot org and we'll have this message easy. Must be good. And then if not we will do return console dot log. Mhm. The movie must not be good. Alright. And then let's caller function down here is movie good. We'll do our score. Let's say it is 75 run. Well, they must be good. Actually not. Let's uh that's a justice because this is kind of muddy muddling McGee things. Uh It's making the messy. All right. So let's say score is greater than 50 or score is actually let's see, let's say is movie alright, We're changing things is movie average. Okay. So we'll say if score is greater than 50 or score is less than say movie must be average. And if it's not we'll say movie, it wasn't average. All right. So, you know, change things here. We're doing this on the fly. So, function name is now is moving average. So there is a movie average. Let's pass it an average score. Let's say it's 65 to a hit run. Movie. I'm still not sure why it's giving us both. So if movie score mhm Is greater than 50 Or score is less than 80, movie must be added for average, I'm not sure why this is giving us both. Okay, well, alright. So I tucked this into a proper else statement. So that was giving us an issue here. So tucked inside a proper out statements. And now it is giving me just the score we want. So movie must be average. Let's say the movie is rated Yeah, 45% on rotten tomatoes. Why is it still saying well, because this is Flawed. This is a flawed system because anything is gonna be less than 80. Alright. We'll ignore my flawed system. Let's say it is 85. Okay. Yeah, this is flawed because it is um It's going it's anything above 80 or anything about 50 in anything less than 80. So uh Huh. All right. Well, so this is not a very good example. I apologize. Now we are just going to deal. We are going to just simplify it. We're gonna do a new function compare X. So if X is mm hmm say less than or equal to five or greater than equal to 15 to 20, You're an equal to 20 or less than or equal to ah ST 10 return council that log Okay. I like this number because for some reason in this scenario you only like numbers between 11 and 19. Okay, cool. Let's do it. I'll stay with you 20 council.org. I hate this number. Yeah, the words narrow, but let's just roll with it. All right. So let's say compare color function. Let's say Pat, we'll pass it 14 and okay, well, so this is a using greater than numbers is not going to be a good example. Alright, let's see here. Okay. Let's see if X equals read. All right. We're just gonna do strings were doing strings here. Honestly, I apologize for not having a better example foolproof checked before I started this guy up. But uh, so if X equals red or X equals well, name of colors, right? Color equals red or color equals blue. Mm hmm. I will say I like this color. Alright. You'll say I hate this color. Let's say it's red. So what is left hand side and assignment. Oh my gosh. Here we go. Perfect. Alright, after all these airs it's amateur hour over here. I am so sorry. Uh, let's say so since the color is red, I like this color. Let's see if the color is purple. I hate this color. Yeah, I don't purple is a good color. Let's say. There we go because again, it is going to check for these data types. Alright, well that took far far too long. Two. Do an example for the logical or operator, but you get it. I don't know why I was struggling so hard or wanted so hard to use numbers. Strings would have worked right out the gate, but we got it going. Thanks for joining you guys. So you have to deal with that. I'm in the process of packing up my entire apartment for a cross country booth. So if I'm a little out there, a little tired because I am right now. But uh, I promise next time I will double and triple check these problems before I present them to you as examples. Alright, well, thank you so much for tuning in and dealing with this nonsense. I'll see you guys next time.
111 Views 0 Likes 0 Comments

Let's learn about more comparison operators in JavaScript!

Comment
Leave a comment (supports markdown format)