Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions Calculator/Pratik1968/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ let value = ""

let ans = 0


function setInput() {
let input = document.getElementById("ans");
input.value = value
}

window.onload = function() {
window.onload = function () {
setInput()
}

Expand All @@ -22,7 +21,6 @@ function NumberButtonClick(number) {
value += number
}


setInput()
}

Expand All @@ -35,7 +33,6 @@ function isOperator(value) {
}

function functionButton(Function) {

let input = document.getElementById("ans");
if (input.value == "" || input.value == null) { return; }
let lastChar = value[value.length - 1];
Expand All @@ -44,7 +41,6 @@ function functionButton(Function) {
return;
}


switch (Function) {
case functionVar.add:
if (lastChar !== "+")
Expand All @@ -59,7 +55,12 @@ function functionButton(Function) {
value += "x";
break;
case functionVar.equal:
ans = eval(value.replace("x", "*").replace("%", "*1/100").replace("^", "**"));
ans = eval(
value
.replace(/x/g, "*")
.replace(/%/g, "*1/100")
.replace(/\^/g, "**")
);
value = ans;
break;
case functionVar.division:
Expand All @@ -81,12 +82,15 @@ function functionButton(Function) {
value = value.slice(0, -1)
break;
case functionVar.decimal:
if (lastChar !== "." && !value.includes('.'))
let parts = value.split(/[\+\-\x\/\%\^]/); // split by any operator
let currentPart = parts[parts.length - 1];

if (!currentPart.includes(".")) {
value += ".";
}
break;
}

}

setInput()

}
}