Transcript from the "Equals" Lesson
[00:00:00]
>> Brian Holt: Lets do equals. Okay, so we're gonna add another case, and handle symbol. Case =,
>> Brian Holt: Okay? And then here we're gonna do two things
>> Brian Holt: if,
>> Brian Holt: So if you wanted to here, you could do like handleEquals if you wanted to or you can inline the code directly here.
[00:00:37]
Let's go ahead and just do,
>> Brian Holt: Handle.
>> Brian Holt: Well, I have it here just being in line, so we're just gonna do it here in line. We're gona say if previous operator.
>> Brian Holt: So let's talk about null for a second, so I'm gonna say if previous equal operator triple equals the thing called null.
[00:01:07]
>> Brian Holt: So null is the total absence of something, right? So in this case, we don't have a previous operator so it's going to say it's null, right? It's not zero. Zero is the quantity of having zero, right? You have zeroes of something. Null is the total absence of something.
[00:01:26]
>> Brian Holt: It's a weird hair to split, but it's one that computer scientists have chosen to split. So in this case, we're just gonna say if the user's on their first time, we're gonna say previous operator is null.
>> Brian Holt: This means the user hasn't chosen an operator yet, which means it's their first time doing it, right?
[00:01:45]
So that's what null is. It's the null option of this which is the previous operator was either divides multiply minus plus or null which means it doesn't have a previous operator. So if the previous operator was null that means they haven't chosen one yet, which means that you need two numbers to do math.
[00:02:06]
You need two numbers to do math, right, which means you should just return here.
>> Brian Holt: Otherwise you're gonna flush operation with the buffer.
>> Brian Holt: So flush operation right there.
>> Brian Holt: Right because if they press equals, you wanna do all of the math up to that point and then give it back to them.
[00:02:33]
Makes sense?
>> Speaker 2: Would you do that again? [INAUDIBLE] flush that?
>> Brian Holt: So,
>> Brian Holt: Whenever someone hits equals, we wanna actually do the math they've been doing, right? So showing you here on this If I hit 25 plus three and I hit equals, I need to do that math, right?
[00:02:53]
I haven't done that math yet, so I need to flush the operation, which is the thing that I previously wrote for if they hit like minus or times or divide, right? I'm just gonna reuse that same function so that when they hit equal, it actually does that math.
[00:03:08]
So flush operation it takes a parameter which is an integer buffer, so it expects by the time that it gets here that this is gonna be a number, so what we're gonna have to do is we won't have to say parseInt but the buffer.
>> Brian Holt: So, that's actually gonna do the math parts of this.
[00:03:44]
>> Brian Holt: What else do we have to do when someone hits equal?
>> Brian Holt: Well, they no longer have a previous operator, right? So if I hit 25 plus 3 and then I hit Enter, there's no previous operator in here anymore, so we're gonna have to clear that out, so we're gonna have to say previous operator equals null.
[00:04:21]
>> Brian Holt: We want to display to the user their answer, right? That's kind of the damn point of the calculator, so we're gonna say the buffer is equal to the running total. That's what we wanna show to the user, and then the actual running total since the math problem is now over, we're gonna assign the zero.
[00:04:47]
>> Brian Holt: Okay, and then we're gonna break after that. Coming back over to this calculator, going to save this I'm going to refresh. Say 25 +3= 28. Come back over here, refresh. Now if I hit 25 times by 3 equals, it is correctly mathing, which is the way that you have to say that when you're programming.