JavaScript Calculator tests failing but getting the proper result

I ran the tests on my javascript calculator and they are failing even though i get the proper result when inputting them into the calculator. Im not sure how the tests work maybe because i have two ids of display with diff variables it messes them up.

for test 9. 5-9+5 = 1 if i type this into the calculator and hit equals i get 1
12. 5*5.5= 27.5 also get the right answer when i type this in
same for tests 13 and 14 i get the correct results when i type them directly into the calculator so i am unsure why these would fail.

here is an link to the calculator:

1 Like

You can’t have two elements with the same id, that’s technically invalid HTML and it is a cause of your testing problems.

1 Like

Alright i’ve changed the code to just have one element with an id of display and it didn’t change any of the test results. I have tried putting the id on the outer div to cover both variables and on each of the divs that contain the output state but the tests still dont pass. The calculators output seems perfect except that the zero doesn’t initially disappear after the calculator is reset.

Read the red text in the test results very carefully:

I just pressed the clear button on your calculator and the element with id of display does not have the value of 0.

Yeah i know that i also figured out what the prob was with the other tests my calculator wasn’t clearing the result after equals was pressed and if a number was typed it just added that number to the previous result. So now all the tests pass except for the display not showing zero which i already knew wasnt right

I guess it is not designed to look at two different variables at once if I name the div surrounding the two variables display only 9 tests pass for some and the reset test passes. If name the div for my calculations display 15 tests pass but the reset test fails. I give up for tonight ill work on this again tommorrow.

I was able to have the variable initialize with zero upon hitting the reset but now fewer tests are passing. I am pretty sure they are failing because i am getting an error (SyntaxError: expected expression, got ‘*’) but i have no idea what is causing this error. Has anyone seen this error before?

here is a link to my code:

Ok it makes sense I’m going to see if I can get it working without updating all the code. That explains why my console.logs where off when i was trying to troubleshoot the code.

Im still not completely understanding how to make sure i am using the current state i understand that the states are updated in batched so i changed my state set states to the functional version but still it hasn’t changed my results not passing. I have tried using componentDidUpdate at one point but i get errors.

Also is there a good way to troubleshoot code using console log when working with react it always seems one state behind maybe that is the problem with my code tho. I was just using the output screen when i wrote the code so it seemed to work perfectly.

here is the updated code not many changes:

Yeah i have changed my code around so many times it has become a bit sloppy.

I put a console log in the function when equals is hit and it shows the correct input and i get the correct output on the calculator so im confused as to why its is still failing. Also i do use this.state.id to keep track of decimals so they cant be repeated. Also yeah i still know some of the regex is unnecessary but i works ok.

I changed the code so everything in my equals function is within the functional setstate. so now everything in my code is in functional setstates but still doesnt work. Is there a way i can tell using console logs if it is working correctly. Ive only been programming for two months now so im still pretty much a beginner at this. I really appreciate your help though.

Yeah i see now. Im better off using regular variables I guess.

Thanks for all the help i was able to get everything working by using regular variables like you suggested.

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