# JavaScript Calculator user stories failing even though everything works perfectly

**URL:** <https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628>\
**Category:** I Built This\
**Created:** [August 20, 2021, 9:30am UTC](https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628 "2021-08-20T09:30:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moro-Afriyie](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@Moro-Afriyie](https://forum.freecodecamp.org/u/Moro-Afriyie)\
**Post date:** [August 20, 2021, 9:30am UTC](https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628/1 "2021-08-20T09:30:45Z")

</div>

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 (codepen.io)](https://codepen.io/moro-afriyie/pen/WNjVjdb?editors=0010)

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [August 20, 2021, 4:24pm UTC](https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628/2 "2021-08-20T16:24:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Moro-Afriyie](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@Moro-Afriyie](https://forum.freecodecamp.org/u/Moro-Afriyie)\
**Post date:** [August 20, 2021, 5:42pm UTC](https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628/3 "2021-08-20T17:42:42Z")

</div>

Alright thank you. 😊

---

<div class="post-metadata">

**Author:** ![Moro-Afriyie](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@Moro-Afriyie](https://forum.freecodecamp.org/u/Moro-Afriyie)\
**Post date:** [August 20, 2021, 8:25pm UTC](https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628/4 "2021-08-20T20:25:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [August 20, 2021, 8:41pm UTC](https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628/5 "2021-08-20T20:41:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Moro-Afriyie](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@Moro-Afriyie](https://forum.freecodecamp.org/u/Moro-Afriyie)\
**Post date:** [August 20, 2021, 8:53pm UTC](https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628/6 "2021-08-20T20:53:49Z")

</div>

OK thanks. I really appreciate it 😊

---

<div class="post-metadata">

**Author:** ![Moro-Afriyie](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@Moro-Afriyie](https://forum.freecodecamp.org/u/Moro-Afriyie)\
**Post date:** [August 20, 2021, 9:56pm UTC](https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628/7 "2021-08-20T21:56:05Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [August 20, 2021, 10:37pm UTC](https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628/8 "2021-08-20T22:37:37Z")

</div>

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

```auto
if(arrValue.length === 1 && arrValue[0] === "0"){
  return;
}

```

But anyway, happy coding ✌

---

<div class="post-metadata">

**Author:** ![Moro-Afriyie](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@Moro-Afriyie](https://forum.freecodecamp.org/u/Moro-Afriyie)\
**Post date:** [August 20, 2021, 10:52pm UTC](https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628/9 "2021-08-20T22:52:04Z")

</div>

Thanks for that, I really appreciate 😃😃

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [February 19, 2022, 10:52am UTC](https://forum.freecodecamp.org/t/javascript-calculator-user-stories-failing-even-though-everything-works-perfectly/474628/10 "2022-02-19T10:52:40Z")

</div>

Closed on February 19, 2022, 10:52am UTC

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