-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (101 loc) · 3.3 KB
/
index.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const currentTyping = document.getElementById("current-typing");
const savedText = document.getElementById("previous-text");
let value = "";
let storedValue = "";
let operatorClicked = "";
let storedTotal = "";
const storeNum = (e) => {
if (!isNaN(Number(e.target.innerHTML))) {
item = Number(e.target.innerHTML);
if (storedValue === "") {
value = value + item;
currentTyping.innerHTML = value;
} else {
value = value + item;
currentTyping.innerHTML = `${storedValue} ${operatorClicked} ${value}`;
}
}
};
const operate = (e) => {
action = e.target.innerHTML;
if (storedTotal !== "") {
operatorClicked === "+"
? (storedTotal = storedTotal + Number(value))
: operatorClicked === "-"
? (storedTotal = storedTotal - Number(value))
: operatorClicked === "X"
? (storedTotal = storedTotal * Number(value))
: operatorClicked === "%"
? (storedTotal = storedTotal / Number(value))
: null;
operatorClicked = action;
savedText.innerHTML = `${storedTotal} ${action}`;
currentTyping.innerHTML = "";
value = "";
} else if (storedValue === "") {
operatorClicked = action;
storedValue = value;
currentTyping.innerHTML = storedValue + " " + action;
value = "";
} else if (action) {
operatorClicked === "+"
? (storedTotal = Number(storedValue) + Number(value))
: operatorClicked === "-"
? (storedTotal = Number(storedValue) - Number(value))
: operatorClicked === "X"
? (storedTotal = Number(storedValue) * Number(value))
: operatorClicked === "%"
? (storedTotal = Number(storedValue) / Number(value))
: null;
savedText.innerHTML = `${storedTotal} ${action}`;
currentTyping.innerHTML = "";
storedValue = "";
value = "";
operatorClicked = action;
}
};
const sumOf = () => {
if (storedTotal === "") {
if (
operatorClicked === "+" ||
operatorClicked === "-" ||
operatorClicked === "X" ||
operatorClicked === "%"
) {
operatorClicked === "+"
? (storedTotal = Number(storedValue) + Number(value))
: operatorClicked === "-"
? (storedTotal = Number(storedValue) - Number(value))
: operatorClicked === "X"
? (storedTotal = Number(storedValue) * Number(value))
: (storedTotal = Number(storedValue) / Number(value));
}
currentTyping.innerHTML = storedTotal;
value = "";
storedValue = "";
} else {
operatorClicked === "+"
? (currentTyping.innerHTML = storedTotal + Number(value))
: operatorClicked === "-"
? (currentTyping.innerHTML = storedTotal - Number(value))
: operatorClicked === "X"
? (currentTyping.innerHTML = storedTotal * Number(value))
: (currentTyping.innerHTML = storedTotal / Number(value));
}
savedText.innerHTML = " ";
};
document.getElementById("clear").addEventListener("click", () => {
value = "";
storedValue = "";
storedTotal = "";
operatorClicked = "";
currentTyping.innerHTML = "";
savedText.innerHTML = " ";
});
Array.from(document.querySelectorAll("button")).forEach((button) =>
button.addEventListener("click", storeNum)
);
Array.from(document.querySelectorAll(".action")).forEach((a) =>
a.addEventListener("click", operate)
);
document.getElementById("equals").addEventListener("click", sumOf);