Calculator project not passing tests

My calculator seems to be working correctly, but the tests claim that even basic functions such as “clear” are not working.

I have a live demo at React Calculator . It utilizes React 17.

Rather than posting source here, I assume folks could just view the source in the demo.

1 Like

It seems from the results that the test suite is having trouble clicking the buttons. That’s just my guess though :frowning:

Hi @brunnock

I am new with React so not easy to understand it, but shouldn’t your reducer switch have a default case default: state ? I mean, not need to return it in the function, am I right?

It looks like your app is not really working fine, it is not reseting the state I guess and it is not handling negative numbers, but not sure what the problem is. I hope this help you in some way.

I would need to test it myself to come with some ideas, I still need practices. :sweat_smile:

Good luck!

It’s good form, but not necessary. In the end, the reducer returns the new state.

I cannot see the code. Hw do you access it?

View Source. The code is in the file calculator.jsx. Hre’s a direct link- calculator.jsx

I think since your using your Digit class instead of the button element the test suite is giving problems. I see that the Digit class is a button but I recommend that you should try to replace the Digit with button.

When running the tests, the display changes. So, my code is responding.

Digit is a just a React component which returns a button. If you use the inspector, you can see that all of the keys are buttons.

It really helps if you post the code using something like StackBlitz or CodeSandbox. So people can actually test the code properly.

If I move all the components out of Calculator and pass down dispatch it passes all but test 13

The sequence “5 * - 5” = should produce an output of “-25” : expected ‘20’ to equal ‘-25’

I’m afraid I don’t understand.

Wait a minute, I think I understand.

Move all the button components out of the Calculator component and pass dispatch down as a prop to each of them.

I would generally suggest you do not declare components inside components. It can often lead to state bugs or issues with re-renders.

Also, I’d really suggest you use something like Vite and not CDN links.

1 Like

OK. I understand and I fixed the code so it passes everything but #13, but that’s another issue. Thanks.

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