diff --git a/Calculators/Basic-Calculator/index.html b/Calculators/Basic-Calculator/index.html index b3f165cb0..6e41f824b 100644 --- a/Calculators/Basic-Calculator/index.html +++ b/Calculators/Basic-Calculator/index.html @@ -33,7 +33,7 @@ 6 % - . + . 1 2 3 diff --git a/Calculators/Basic-Calculator/script.js b/Calculators/Basic-Calculator/script.js index 814f943d8..0a81d92d0 100644 --- a/Calculators/Basic-Calculator/script.js +++ b/Calculators/Basic-Calculator/script.js @@ -8,7 +8,15 @@ let display = document.getElementById("display"); document.addEventListener("keydown", function (event) { const validKeys = /^[0-9.\+\-\*\/\(\)\^\%\{\}\[\]&]$/; if (validKeys.test(event.key)) { - document.getElementById("display").value += event.key; + if (event.key === ".") { + console.log(event.key) + event.preventDefault(); + point(); + } + else { + event.preventDefault(); + document.getElementById("display").value += event.key; + } } else if (event.key === "Enter") { event.preventDefault(); calculate(); @@ -46,3 +54,31 @@ function clearOne() { let currentValue = calc.txt.value; calc.txt.value = currentValue.slice(0, -1); } + +function point() { + let currentValue = calc.txt.value; + if (currentValue == "") { + calc.txt.value = '0' + '.'; + } + else { + + if (currentValue.charAt(currentValue.length - 1) == "." || currentValue.charAt(currentValue.length - 1) == "(") { + calc.txt.value = calc.txt.value + '0' + '.'; + return; + } + if (currentValue.charAt(currentValue.length - 1) == "+" || + currentValue.charAt(currentValue.length - 1) == "*" || + currentValue.charAt(currentValue.length - 1) == "-" || + currentValue.charAt(currentValue.length - 1) == "/" || + currentValue.charAt(currentValue.length - 1) == "%" || + currentValue.charAt(currentValue.length - 1) == "√" || + currentValue.charAt(currentValue.length - 1) == "^") { + calc.txt.value = calc.txt.value + '0' + '.'; + return; + } + else { + calc.txt.value = calc.txt.value + "."; + return; + } + } +};