Javascript Calculator: How to make "equals" work

I am working on

It’s in React and I am stuck with User Story #9 . What logic should I use in order for the equal(=) sign to work?

Also, any suggestion on how I can improve the logic in my handleOne , handleTwo, handleThree handleAdd etc.

here is my code:

Sorry if its messy.

I did the JavaScript Calculator before the new curriculum (so I didn’t use React), but the general consensus was when doing this project you can deal with the numbers and arithmetic statements like a string (as in your CodePen), and then use the JavaScript eval() function to evaluate the JavaScript code represented as a string. For example:

console.log(eval('2 + 2'));
// expected output: 4

Not sure what general consensus your are referring, but this project can definitely be solved without using eval at all. In general, when you are taking input from a user, it is best practice to avoid using eval when displaying back anything involving the input received.

The JavaScript calculator does not actually involve accepting text from an input field. You are just clicking buttons.

So no malicious code can be input and ran. But being cautious when using eval is something to keep in mind.


I will try to avoid blanket statements about the “general conscientious of FreeCodeCamp”. What I meant was I read a lot of project reviews of the JavaScript Calculator a while back, and using eval() was controversial but in the end a lot of people used it because it was safe if no Input control was used.

1 Like

Thank you for the the replies.

I used eval because it works for me and next time I will look for other ways to solve this without eval .