forked from muddhit/Hack-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
70 lines (61 loc) · 2.08 KB
/
script.js
File metadata and controls
70 lines (61 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const ouputContainer = document.querySelector(`.output-value`);
const prevValueContainer = document.querySelector(`.prev-value`);
const getFormattedNum = (num) => {
if (num == "-") return "";
const n = Number(num);
const value = n.toLocaleString("en-IN");
return value;
};
const getUnFormattedNumber = (num) => {
return Number(num.replace(/,/g, ""));
};
const getOutputNum = () => {
return ouputContainer.innerText;
};
const getHistoryNum = () => {
return prevValueContainer.innerText;
};
const operatorsList = document.querySelectorAll(`.operator`);
Array.from(operatorsList).forEach((operator) =>
operator.addEventListener("click", function () {
if (this.id === "clear") {
prevValueContainer.innerText = "";
ouputContainer.innerText = "";
} else if (this.id === "backspace") {
const num = getUnFormattedNumber(ouputContainer.innerText);
ouputContainer.innerText = getFormattedNum(num.toString().slice(0, -1));
} else {
let output = getOutputNum();
let history = getHistoryNum();
if (output == "" && history != "") {
history = isNaN(history[history.length - 1])
? history.substr(0, history.length - 1)
: history;
}
if (output != "" || history != "") {
output = output === "" ? output : getUnFormattedNumber(output);
history += output;
if (this.id == "=") {
prevValueContainer.innerText = "";
ouputContainer.innerText = getFormattedNum(eval(history));
history = "";
} else {
history += this.id;
ouputContainer.innerText = "";
prevValueContainer.innerText = history;
}
}
}
})
);
const numbersList = document.querySelectorAll(`.number`);
Array.from(numbersList).forEach((operator) =>
operator.addEventListener("click", function () {
let outputVal = getUnFormattedNumber(getOutputNum());
if (outputVal !== NaN) {
const num = outputVal + this.id;
if (num.length > 9) ouputContainer.style.fontSize = "30px";
ouputContainer.innerText = getFormattedNum(num);
}
})
);