DEV Community

Discussion on: Daily Challenge #10 - Calculator

Collapse
 
mkrl profile image
Mikhail Korolev

Alright, I'm gonna be the one who posts this.

const calc = exp => eval(exp)
Collapse
 
alvaromontoro profile image
Alvaro Montoro • Edited

If you add a check to verify the string is well formed, this will probably be the best and simplest answer.

Collapse
 
yzhernand profile image
Yozen Hernandez • Edited

Similarly unfair, the solution in R:

calc <- function (expr) {
    eval(parse(text=expr))
}
> calc("2 / 2 + 3 * 4 - 6")
[1] 7

Warning: don't do this.