More React practice: creating a calculator

This is what I have so far; it’s not much:

I have several questions.

  1. Where do I code the runningTotal…? Right now I have it in handleCalc so I can keep it updated as inputs are added. But maybe it makes more sense to have it in handleResult when the user clicks the = button…?

  2. I have collected all the inputs from calculator as an array called inputs. And now I want to reduce that array to a single value. Can I use reduce or should I make my life easier and use a for loop…?

Thanks in advance.

1 Like

you are going good man , even i try to build with react but am too begineer so i couldn’t and i did with the basic html,css and js, here is my project

you will get some idea
Hope You will succed
Happy coding

1 Like

First, great job on the calculator!

Take my reply with a grain of salt as I am still learning React myself but it makes more sense in my mind to handle the total in handleResult and update after the user clicks the = button.

reduce is a great way to get a total from the array of inputs…it’s also cleaner with less chances to make syntax mistakes or mathmatical errors. My philosophy is ‘work smarter not harder’ and reduce does all the heavy lifting for you with less code so that would be my suggestion :smiley:

I can’t wait to see the finished project!

1 Like

Final product:

I saw on the message boards that “eval” is not always appropriate to use but it was not clear to me whether this was one of those times or not…?

Also, not terribly happy with the UI but that’s on my list of things to keep learning.

Feedback welcome.