Build a JavaScript Calculator

Hello everyone,
Glad to be part of this forum, this is my first post so I look forward to interacting you. Please I need help. I have almost completed the code for this project Build a JavaScript Calculator . I used JavaScript eval() built in function for calculations.
The issue is that I have 4 out of 16 tests which are failing. I only expect one to fail because I have manually tested the calculator using tests in the stories and it seems to work fine. For example, test 9

User Story #9: In any order, I should be able to add, subtract, multiply and divide a chain of numbers of any length, and when I hit = , the correct result should be shown in the element with the id of display .

When I try the calculator manually I think this test is supposed to pass but it keeps failing. I was only expecting one test 13 since I haven’t added that functionality yet. Please help me resolve this issue. Is it okay for me to share the codepen link here?

You’ll need to share your code, is it on codepen or another platform?

Thanks for reply. It is codepen, Here is the link
https://codepen.io/billjay/pen/rNbEBQb
Please I really need your help. I am using React-Redux but I haven’t extracted state from react component to redux yet.

1 Like

I’ve managed to pass the test.
So the test expects that the div with id display has only the result when you press ‘=’.

<div id="display">
     {this.state.answer || <div>{this.state.input !== ''? this.state.input + '\n' : '0\n'}</div>}
</div>

The div element was doing too much. I’ve added this check which means:

If there is an answer then show the answer otherwise show the equation,
to hide the equation when we have a result.

There is one more test failing but that is for you to figure it out.
I don’t want to spoil your exercise.


I initially thought the problem was related to usage of eval but that was a red herring.

Wow, that’s wonderful! Thanks so much for your help GeorgeCrisan, I really appreciate it. It took me days trying to debug this.
It works fine now. Let me proceed and finish up with the last test.

Thanks again

1 Like