diff --git a/BasicCalculator/index.html b/BasicCalculator/index.html index e8fa87a4..20e9ad8d 100644 --- a/BasicCalculator/index.html +++ b/BasicCalculator/index.html @@ -4,7 +4,7 @@ - + Calculator @@ -24,6 +24,7 @@ +
@@ -38,6 +39,6 @@
- + \ No newline at end of file diff --git a/BasicCalculator/script.js b/BasicCalculator/script.js index 947e5f6b..9763cc0a 100644 --- a/BasicCalculator/script.js +++ b/BasicCalculator/script.js @@ -7,28 +7,30 @@ const getkey = (key)=>{ if(key == '='){ output_win.innerHTML = "INVALID OPERATION"; expression = ""; + firstVal = true; } - else{ expression = key; - output_win.innerHTML += key; + output_win.innerHTML = key; firstVal = false; } } else{ if(key != '='){ expression += key; - output_win.innerHTML += key; + output_win.innerHTML = expression; } else{ try{ - - output_win.innerHTML = eval(expression).toString(); + let result = eval(expression).toString(); + output_win.innerHTML = result; + expression = result; + firstVal = false; } - catch(err){ output_win.innerHTML = "INVALID OPERATION"; expression = ""; + firstVal = true; } } } @@ -37,15 +39,48 @@ const getkey = (key)=>{ const getClr = ()=>{ output_win.innerHTML = ""; expression = ""; + firstVal = true; } const leftPar = ()=>{ lp = document.getElementById('btn-leftPar').value; - output_win.innerHTML += lp; - expression += lp; + if(firstVal){ + expression = lp; + output_win.innerHTML = lp; + firstVal = false; + } else { + expression += lp; + output_win.innerHTML = expression; + } } const rightPar = ()=>{ rp = document.getElementById('btn-rightPar').value; - output_win.innerHTML += rp; - expression += rp; + if(firstVal){ + expression = rp; + output_win.innerHTML = rp; + firstVal = false; + } else { + expression += rp; + output_win.innerHTML = expression; + } } + +const getDecimal = ()=>{ + if(expression == "" || expression == undefined || firstVal){ + expression = "0."; + output_win.innerHTML = "0."; + firstVal = false; + } + else{ + + let lastOperatorIndex = Math.max(expression.lastIndexOf('+'), expression.lastIndexOf('-'), + expression.lastIndexOf('*'), expression.lastIndexOf('/'), + expression.lastIndexOf('(')); + let currentNumber = expression.substring(lastOperatorIndex + 1); + + if(!currentNumber.includes('.')){ + expression += "."; + output_win.innerHTML = expression; + } + } +} \ No newline at end of file diff --git a/BasicCalculator/style.css b/BasicCalculator/style.css index ce78b69c..f561d68f 100644 --- a/BasicCalculator/style.css +++ b/BasicCalculator/style.css @@ -56,7 +56,7 @@ width: 50%; background-color:azure; display:grid; - grid-template-columns: 50% 50%; + grid-template-columns: 33.33% 33.33% 33.33%; place-items:center; box-sizing: border-box; border-radius: 10px;