JavaScript Calculator user stories failing even though everything works perfectly

My calculator seems to work perfectly well but it’s failing to pass some user stories event though it’s working perfectly. Can anyone help ? i’m failing user story 9,12,13 and 14. But when you use the calculator, it works very fine except for the fact that it can’t handle multiple zeros after the operator sign, is there a way i can also fix that bug?

link to the code Calculator (

Carefully reading the error messages often helps to identify the problem, for example this:

  1. I should be able to perform any operation (+, -, *, /) on numbers containing decimal points
    The expression “10.5 - 5.5” should produce an output of “5” : expected ‘10.5 - 5.5 = 5’ to equal ‘5’

This means that you’re putting the whole string ‘10.5 - 5.5 = 5’ into the <div id="display">, but it should be only the result.

It’s possible that you suddenly pass all tests when you fix that.

1 Like

Alright thank you. :blush:

So I fixed that bug and the only error I’m getting is handling multiple zeros, is there a way I can do this?

You’re already doing some sort of input validation for the decimal point, which is working fine. You can surely do something similar for multiple zeroes? Something along the lines “if the current number is zero, don’t allow entering more zeroes”. Neat styles btw.

1 Like

OK thanks. I really appreciate it :blush:

I fixed it with this line of code and all the test cases passed. thanks for everything @jsdisco :raised_hands: :raised_hands:

// handles the multiple zeros bug
    else if (value==="0") {
    const arrValue = currentValue.split("");
      if((arrValue.length>0 && arrValue.length<=1) && arrValue[0]==="0"){

You could simplify it a bit by changing the if condition:

if(arrValue.length === 1 && arrValue[0] === "0"){

But anyway, happy coding :v:

1 Like

Thanks for that, I really appreciate :smiley::smiley:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.