Javascript Calculator not passing tests 11 and 12

Here’s my codepen
I can’t pass tests 11 and 12 even though more than one decimal points are not appearing and I can operate on decimal numbers. Please help.

Have you tried multiplying an integer by a float? For example, 6 * 4.2 – see what you get.

Yes. I am getting 25.200000000000003

Huh. When I do that, I get 0

Nope, scratch that. It’s when I’ve hit =, get a result, then try to use that result for another computation.

I’ve rectified it. Tests are still not passing.

I think part of the problem can be rectified by shortening your precision – it tells me that 5.55 - 5 = .549999999998. If you used some rounding mechanism, to perhaps their suggested four-point precision, you may solve some of the issues.

Nope. Still not working.

Wow. THAT was a pain. Wow. Dang. I figured out the problem. Dang. Wow.

So it seems that there is a distinction between the decimal point and the period. Display-wise, they look the same, but event.which on them both, and the decimal point on the numeric keypad has a keycode of 110, while the period’s keycode is 190. event.code on both are different as well, Period vsNumPadDecimal.

As far as the tests go, it seems they are using one in some, and the other in some. Try both here

As to why this bit you and only you, I’m not sure. Perhaps because you’re handling the click events and the keyboard events with the same handler, but only creating a key property on the click events? I’m still researching, but that seems to be where it is biting you.

To determine where the error was, I put in a console.log showing all the values, every time the calculate() was called, allowing me to monitor the state of each in real(ish) time. I could have done something similar with chrome’s watch tool (in the sources tab, chrome dev tools), but this was my quick and dirty.

EDIT: Okay, that’s weird. The tests are clearly simulating clicks on the buttons, which would generate the ‘.’ character, and they both have the charCodeAt() of 46, but when I type the period (rather than the numeric decimal), it doesn’t display. That is FUNKY.

Never mind, I’m an idiot. Take a look in your AC key handler – you never set dec to an empty string, so if you had a decimal and clear all, it still says you have a decimal.

I WAY overthought that.

1 Like

Oh man! Just a dec = ’ ’ causes so much pain in the a**. Anyway thanks for the help. You clearly spent considerable time figuring it out for me. Sorry about the poorly formatted code. What started as a quick one hour exercise ended up taking half my day! Cheers!

So a suggestion for future projects? If you do something like this, don’t rely on another unrelated variable when you don’t need it.

Your if statement toggles on dec, which is arbitrary and has no correlation to either value. It makes no sense. Instead, toggle on whether the display shows a period. Simply do something like

if (!document.getElementById(“display”).innertext.includes(".") ){ // no period }

1 Like

That’s a solid suggestion. Thanks!