en.javascript.info/1-js/99-js-misc/02-eval/1-eval-calculator/solution.md
2019-06-22 10:17:44 +03:00

387 B

Let's use eval to calculate the maths expression:

let expr = prompt("Type an arithmetic expression?", '2*3+2');

alert( eval(expr) );

The user can input any text or code though.

To make things safe, and limit it to arithmetics only, we can check the expr using a regular expression, so that it only may contain digits and operators.