Calculator Project Help

Hey guys, I’ve been stuck for a bit in the calculator project. It’s been really challenging.

This is my code on codepen:

The known issues that I’m facing are:

  • Can’t keep making operations after the first one (I don’t know where to reset my currentValue variable, and which value should I give it)

  • If I click the numbers buttons after making an opperation and having the totalValue displayed, it’ll add a number to the right of the current display value (totalValue), instead of reseting it.

I don’t know how good is my approach to the project and would love to hear some toughts and have some feedback so I can improve my logic and skills.

All the help is deeply appreciated!

I looked through the core briefly, on mobile. But given what you describe, and what I experienced with it, you want to save any final results after a operation in a separate variable, then clear the display be resetting the display value. So for example with the user enters 5, you save 5 as the first input number. When the user enters +, you then determine what function you need to call (addition), and then the user enters 10, you save that as a second input. When the user presses enter, then you call the addition function sending the two variables. You the save the result as it’s own variable, runningTotal or something. You print that to the display, and the you either wait for another operation button to be pressed, or a number. If a number is pressed, you clear the display first, (updateDisplay = “”) then print the number (pretty much treat it as the user is starting a new calculation) and save that number as the new input 1). Right now you are just concatenating new numbers. You can have a check to seen if equal has been pressed, if so you can clear current value when a number is pressed and then begin adding numbers to the display.

If the user enters another operation, say minus. Then you transfer the runnjngTotal to input one, and continue this way.

I know there isn’t much detail specific to your case, but hard to mess around on codepen on mobile.

Thanks! I will try to aproach the exercise in that manner and look for flaws in my logic!
Much appreciated. I’ll return here to update my situation.

1 Like

Hey man just passing by to let you know that you helped me finish it!

What a challenge, but so happy to finally make it work. Soon I’ll add the extra implementations, but the main logic is working!

Thanks for your help!

So glad you got through it. I spent weeks finishing mine. Learned a lot on it.