Check out a free preview of the full Calculator Project: JavaScript course:
The "Equals" Lesson is part of the full, Calculator Project: JavaScript course featured in this preview video. Here's what you'd learn in this lesson's course:

Brian adds the functionality for the equals button to the calculator.

Get Unlimited Access Now

Transcript from the "Equals" Lesson

>> 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.

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.

>> 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.

>> 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?

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.

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.

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?

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.

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.

>> 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.

>> 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.

>> 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.