JavaScript Calculator - User Story #14 - help - SOLVED

I’ve built a Calculator. It passed all tests but one (test No.14).

User Story 14: Pressing an operator immediately following = should start a new calculation that operates on the result of the previous evaluation.

It seems that calculator does fulfill this user story but test doesn’t agree :slight_smile:

Does anyone have any ideas what I’m doing wrong?
Any help would be appreciated :slight_smile:

Link to calculator:

You need to account for the order of operations.

When the sequence is 5 - 2 / 2 =

The expected output is 1.5 but you are outputting 4

@shimphillip that’s incorrect. Order of operations for the sequence you mentioned 5 - 2 / 2 = would be 5 - (2 / 2) = so 4 is the correct answer.

Math order of operations is PEMDAS

@orvalho I’m not sure why the test fails. I tried and it seems your calculator does fulfill the story. Granted, I haven’t done this one yet but I’d expect the results to pass too.


Interesting, the test case that’s being failed says otherwise.

Ohhhh @shimphillip…that’s not what you wrote.

You wrote 5 - 2 / 2 =
the test says
5 - 2 = / 2 = and that indeed should (and does) equal 1.5

And odd, but when I put that in his calc it does correctly output 1.5

@orvalho didn’t mean to hijack your thread. I believe you can skip this test because your answer is correct. It may be that the text of the test was written to be 5 - 2 = / 2 but the actual test is doing 5 - 2 / 2 = which as we know are two different equations.

1 Like

@shimphillip, @Roma thank you both for your insight :slight_smile:
The important thing is that my calculator does fulfill user story (though FCC test doesn’t acknowledge it).Thanks for reassurance :slight_smile:

1 Like

@orvalho You’re welcome.

I know enough JS to be dangerous. I read something earlier that FCC was written for humans by humans so there will be errors. I’ll keep it in the back of my mind to go to GitHub and look at FCC’s test for this and write an issue if one is warranted, which it seems it is.

Good job on your calculator!

1 Like

Here’s the fixed version. Check out the line #36.

The test runs very fast, and it failed because there was a 10 ms delay before you updated your input state again.

Of course, when you do it manually, it worked as expected because we can’t input the whole error causing expression in under 10 ms.

Have a look on this version as well, I just increased the time to 10 seconds so you can see what’s happening and why you are getting 4 as in output in tests.


@husseyexplores thanks for the update. Looks like you guys were on top of it the whole time.

1 Like

Thank you everyone for your help! When I posted my question I thought it was a long shot and didn’t expect that anyone would take time to go through my code as I couldn’t pinpoint which part of my code was causing test to fail.
@husseyexplores thanks a lot for solving the issue and for great explanation too.