eval()

11 Jan 2018

So my health is not back to normal and I really need to rest. But I’m bored out of my mind!

Tonight I learned about eval() while searching for help on the Free Code Camp forum, in order to complete my calculator project.

Say you have a string and you want to calculate what’s inside the string:

let str = '1+5/2'
let result = eval(str) //3.5

Now there could be security issues with eval() (Imagine someone intercepts the code and inserts commands in the variable to be evaluated) So before I eval, I remove all letters from the string with a regexp.

newStr =str.replace(/([a-z])/gi, "");

That’s all folks! My calculator is working. I have to add the floating point button and all the pretty css. Going back to rest some more now!