JavaScript Calculator - Test Issue

Hi !
I’m having issues in passing the tests on the Calculator Project, even if (almost) everything works. The result on the display is correct and the elements (with all the correct ids) are visualized, but I’m only passing 1 test or 2 (the number changes every time I add or remove something and save).

Here is the link to my project.

Could it be because instead of visualize the result only when the “=” is pressed, I calculate the result after having 2 operands? And if so, why am I not passing only the test related to user story #9?

Thanks in advance!

You are currently not passing many tests, because after clicking Run Tests, your code errors out on the following line of your handleOperation method.

var arr = this.state.display.split(/[\-,\+,\/,x]/);

this.state.display is a number and not a string at this point, so you can not use the split method on it.

Also, I notice I can not add 1.49 + 1.49 with your calculator. I will not allow me to enter the decimal point between the second 1 and 49.

When I ran the tests, your app got a lot of “before all-” and “after all” hook errors.
It seems that according to your App component’s constructor, only two methods are bound to the component (this.handleNumber and this.handleOperation); but there appears to be more (e.g. result, switch and round) inside the component. Maybe try binding all methods to the component first?

Also, if I try to do 2 + 2 * 9, the calculator would interrupt and give me 4 as soon as I hit *; which is not right.

@meowjaja But 2 + 2 is 4. Why is it a problem if the camper’s code goes ahead and converts the 2 + 2 to 4?

I wrote that part under the assumption that the calculator should work with the formula logic (i.e. observing order of operation precedence); but according to the specs, either formula or immediate execution logic is acceptable. So no, that’s not really a problem; OP please ignore that part.
(Sorry for the confusion; it’s been a little while since I finished mine :sweat_smile: )
Thanks for pointing that out @RandellDawson!

1 Like

I didn’t bind those methods because they are not passed to the other components, so there is no needs to… if that was the problem the program would have not run at all… :thinking: I tried anyway to bind them but it was the same. Thanks anyway for your time! :blush:

Yes I’m aware of the problem with the decimal operator, I think I created some kind of bug with all those regex… For the “this.state.display” I’m sure it is a string, after calculating the result in fact I converted it with the .toString() method, I tried to print it in the console and it has the correct type. I think the problem is somewhere else but my code is a mess, probably I’ll just start all over again, trying to make it more clear! :sweat_smile:

Thanks for your help! :blush: