I am working on https://learn.freecodecamp.org/front-end-libraries/front-end-libraries-projects/build-a-javascript-calculator.
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: https://codepen.io/bradrar1/pen/oMNvMd
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
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
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.
2 Likes
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 .