Calculator project and eval()

Good afternoon campers!

I’m about 2-3 hours into the Build a JavaScript Calculator project, and I was hoping to get some insights from people that have completed the challenge.

I’m slowly making progress on the logic for the actual calculation of user-inputted numbers. I’m already over 140 lines of JavaScript and can see this will keep growing. If I used the built in eval() function, I could dramatically cut down on my program’s size (and have it working in a few hours). Sounds like a good move!

But I’ve been looking around, and eval() seems to be universally frowned upon online (and in You Don’t Know JavaScript).

Did you use eval() in your calculator project?

1 Like

If you want to use eval(), you’d probably want to sanitize your input (like, make sure the string only contains characters that’s you’d expect to see in arithmetic, etc).

I didn’t use eval(), but instead used a couple of algorithms to parse and execute the math. You can start here: https://en.wikipedia.org/wiki/Shunting-yard_algorithm

3 Likes

Glad to hear someone managed to get through it without using eval().

Could be a good chance to learn a helpful algorithm.