Javascript Calculator Problem with eval()

Javascript Calculator Problem with eval()

I know I KNOW! lol I should not be using eval() to solve this challenge, at least that’s what I keep reading on the FCC forum and other places, BUT I am stubborn and will use it haha

I have passed 15/16 user stories on this challenge but I can’t get #13 to pass.

eval() does not process the (5 * - + 5)equation like FCC wants me to, when I try editing my state to process the equation like FCC wants it, other user stories fail. I keep going in circles.

Can anyone help me out please? Link Below

Cheers and thank you! :grin:

JS Calculator on CodeSandbox

It can’t work because 5 * - + 5 isn’t valid JS or a valid maths calculation. The solution is either don’t accept input that is invalid (so you possibly need a record of what the user has done), or sanitize the input before running eval (so look at the string and running some replace function on it). You need to figure out what to do when someone just keeps hammering operator keys, because you can’t do anything with the input when it looks like the above.

Thanks for helping out Dan, greatly appreciate it.

I was able to pass [#13] with the advice you provided but now [#14] Pressing an operator immediately following = should start a new calculation that operates on the result of the previous evaluation. is failing (wasn’t failing before), the first equation works great but after I hit the = to try and use the value I got from my first equation, it crashes my app.

Any thoughts on this? thanks again!

I figured it out, the eval() produces a number and my code is expecting a string, just used the toString() to get it to pass, project completed! thanks!!!

1 Like