From 8b567f537becc7f69b02af774f782f8a5abe8f84 Mon Sep 17 00:00:00 2001 From: Osvaldo Dias dos Santos Date: Fri, 20 Jan 2023 22:13:02 +0100 Subject: [PATCH] Add solution file --- 1-js/99-js-misc/02-eval/1-eval-calculator/solution.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 1-js/99-js-misc/02-eval/1-eval-calculator/solution.md diff --git a/1-js/99-js-misc/02-eval/1-eval-calculator/solution.md b/1-js/99-js-misc/02-eval/1-eval-calculator/solution.md new file mode 100644 index 000000000..24d40c9b8 --- /dev/null +++ b/1-js/99-js-misc/02-eval/1-eval-calculator/solution.md @@ -0,0 +1,11 @@ +Let's use `eval` to calculate the maths expression: + +```js demo run +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](info:regular-expressions), so that it only may contain digits and operators.