Need help in front end library 'calculator' project

Hi, I got stuck in this project trying to do it for the last 2 days . I am very new to React and I don’t know what was happening. Can anyone please help me, review my code. My codepen is:

Any help in solving this problem will be highly appreciated Thanks!

Hey, so running your app through the test we get 11/16. I will roughly go over each one, and hopefully you’ll know how to flesh out the details.

  1. test # 7 and 8: You have this error message: ReferenceError: can’t access lexical declaration ‘result’ before initialization
    – this means somewhere in your code, you have used the variable name result before it exists.

An example of what that means:

console.log(result); // result doesnt exist yet
let result = 0;
  1. test # 10 and 14: Both are about getting the right results from a calculation
    – currently inputting the two tests manually into the calculator doesn’t yield the right result either. Meaning somewhere, your calculation logic is broken.

  2. test # 11: If you input 0 repeatedly into your calculator, currently it becomes 000000.
    – The reason is in your handlechange, there’s two big blocks: A: if your calculator is in your initial state, in this state you cannot have leading zeroes. B: if your calculator isnt in your initial state, and you can have more zeroes in the display.
    However, no matter what, after one input I see you change the calculator into a non initial state. This means by your second 0, that leading zero checking logic in A is never used.

  3. Test #15: Something is wrong with how you chain calculations. My guess is after an “=” operation, the result isn’t saved as previous, so the next operation ends up having an operand of "".

A good habit to get into is split your code. Think about what a calculator needs, break those down to components, what each component needs to be able to do, break those down into functions. This way, every time something goes wrong, you can roughly know which function to look through, instead of having to sift through one big function every time.

Good luck

This calculator one seems simple but I think it’s more complex than it lets on. Have you completed the other react challenges already? I have learned a TON about react (including switching to hooks/context instead of the classed components taught in the lessons) by having to read documentation on newer react versions in order to complete the first three challenges of this series.

Thanks, @MatchaCrisp I did changes accordingly and it worked!! 15/16 test passed

1 Like

Thanks for the suggestion Salathor ,I think you are right this problem is complex and I need to learn more about react!! learning about hooks now

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