From 3fdfb96e6d18a52274891486c5abd1cf41353a5a Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Sun, 14 Jun 2020 21:47:35 -0500 Subject: [PATCH] Clean input on input --- js/calc.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/js/calc.js b/js/calc.js index 421b358..1030b18 100644 --- a/js/calc.js +++ b/js/calc.js @@ -5,6 +5,7 @@ let display = document.querySelector("input.view"); let justEvaled = false; +const validOpts = Array() const isNumeric = (val) => { return !isNaN(parseInt(val)); @@ -43,6 +44,24 @@ const init = () => { } }); }); + + document.querySelectorAll('td').forEach((btn) => { + validOpts.push(btn.innerText); + }); }; -init(); \ No newline at end of file +display.addEventListener('input', () => { + let newVal = String(); + + for (let char of display.value) { + if (validOpts.includes(char)) { + newVal += char; + } + } + + display.value = newVal; +}); + +window.addEventListener('load', () => { + init(); +}); \ No newline at end of file