Check out a free preview of the full Calculator Project: JavaScript course

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

Brian implements the code that performs math and then demonstrates how to debug code by logging to the console.

Preview
Close

Transcript from the "Flush Operation" Lesson

[00:00:00]
>> Brian Holt: So let's try flush operation. Flush operation. This is gonna take in the int buffer that we got from above, which is called the same thing for continuity. And then we're just gonna ask a bunch of questions here. We're just gonna say, if previous operator ===+. Again, you could do this with a switch statement as well.

[00:00:28]
I'm just gonna stick to if and else if, that's what my notes say. Then I'm gonna say runningTotal += intBuffer.
>> Brian Holt: It makes sense. If this press +, then they want it to add.
>> Brian Holt: And let's just, I'm gonna copy and paste this a couple of times so else if.

[00:00:55]
>> Brian Holt: Or we can just write it out. Else if previous operator ===-. Then running total, -=. Or you can say = running total- that. But I'm just gonna use -=, yeah?
>> Speaker 2: Could we use the switch statement here?
>> Brian Holt: Absolutely. Yep, I'll stiff previous operator, === and times running total times =.

[00:01:40]
Again, that asterisk is how you do multiplication. Into total. Or not buffer, better. Otherwise else running total /= which is /= and puffer. Okay so this is working.
>> Brian Holt: I notice that it is doing in the correct addition down here like what we would expect. Times by 5.

[00:02:34]
>> Brian Holt: Let's go one step further up here to the handle symbol and see what symbols coming in here. [SOUND] So as you can see here, I use console.log everywhere. Okay, so I'm gonna say.
>> Brian Holt: So it looks like JavaScript is converting these for me. So what we don't want is actually and times here, we actually do want the the symbol here.

[00:03:17]
>> Brian Holt: So if you're using a Mac, that's control command space, brings up this little guy. And you can do multiplication. And it's this one right here that we want, multiplication sign. So here my HTML, I have like the divide and LIRR. I'm just going to right click on this one, the multiplication is the Inspect Element right there.

[00:03:49]
That's universal across Chrome and Firefox and all that. Okay, inspect element. Now, you can see this is highlighted here in my HTML editor. Just double click on it and I'll let you actually edit what's in that HTML. I'm just going to copy it, control copy. And then I'm going to go over to my JavaScript here, and paste that into the places that I had the end times semi colon.

[00:04:18]
You're gonna have to do that with if you have the minus there as well, you can have to do that with minus if you have it there with addition, so on and so forth. You have to do the HTML as well. No don't don't do it in the HTML.

[00:04:35]
How would the function look at those a switch like where would previous operator ago It would look like switch previous operator. Case, plus running total plus equals into buffer.
>> Brian Holt: Break, case- so on and so forth. Does that answer your question?
>> Speaker 2: Yeah, it does.
>> Brian Holt: Cool.
>> Brian Holt: Probably a bit cleaner to do it that way, but either one of these works.

[00:05:19]
>> Brian Holt: So now hopefully if we go in here to console.
>> Brian Holt: Tell it bigger here. So refresh, I would say 5.
>> Brian Holt: Make this a bit smaller, -2 +, so that's correct, 6. If I hit multiplication, I'm gonna expect to see.
>> Brian Holt: 9-, okay so yeah, that seems like it's working, okay.

[00:06:02]
>> Speaker 2: Does it show up on your screen?
>> Brian Holt: We haven't written to the screen yet. We have not written it to the screen yet. So you should not be seeing that yet. So I'm going to get rid of these console logs, cuz now the stuff works the way that I want it to.

[00:06:15]
You can leave them in there, but in general you don't want to ship code to have console logs. It's just kind of a messy thing to do.
>> Brian Holt: It should be for debugging purposes, and then you should get rid of it.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now