-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
137 lines (120 loc) · 3.69 KB
/
script.js
File metadata and controls
137 lines (120 loc) · 3.69 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
let num1 = ``
let operator = null
let num2 = ``
let finalVal
let operators = ["/", "×", "+", "-", "%"]
let display=document.querySelector(".display")
let expression = document.querySelector(".expression")
let buttons = document.querySelectorAll(".keys button")
let finalValue = document.querySelector(".finalValue")
//functions
function add(num1, num2) {
return num1 + num2;
}
function subtract(num1, num2) {
return num1 - num2
}
function multiply(num1, num2) {
return num1 * num2
}
function division(num1, num2) {
return num1 / num2
}
function percent(num1, num2) {
return (num1 / num2) * 100
}
function calci(number1, expression, number2) {
switch (expression) {
case "/":
return division(number1, number2);
case "-":
return subtract(number1, number2);
case "+":
return add(number1, number2);
case "×":
return multiply(number1, number2);
case "%":
return percent(number1, number2);
}
}
Array.from(buttons).forEach((button, index, array) => {
button.addEventListener("click", (event) => {
const value = event.target.innerText;
if (value === "c") {
num1 = '';
num2 = '';
operator = null;
expression.innerText = '0';
finalValue.innerText = ``
return;
}
if (value === "=") {
if (num1 !== '' && operator !== null && num2 !== '') {
finalValue.innerText = ``
let finalVal = calci(+num1, operator, +num2);
if (finalVal % 1 !== 0) {
finalVal = finalVal.toFixed(2);
} else {
finalVal = finalVal;
}
expression.innerText = `${(num1)} ${operator} ${num2}`
finalValue.innerText = `= ${finalVal}`;
num1 = finalVal.toString();
operator = null;
num2 = '';
}
return;
}
if (value === "↩") {
if (num2.length > 0) {
num2 = num2.slice(0, -1);
expression.innerText = `${num1}${operator}${num2}`;
}
else if (operator !== null) {
operator = null;
expression.innerText = num1;
}
else if (num1.length > 0) {
num1 = num1.slice(0, -1);
expression.innerText = num1;
}
if (expression.innerText === '') {
expression.innerText = '0';
finalValue.innerText = ``
}
return;
}
//To assign operator value
if (operators.includes(value)) {
if (num1 !== '') {
operator = value;
expression.innerText = `${num1}${operator}${num2}`;
}
else if (num2 !== ``) {
operator = value;
expression.innerText = `${num1}${operator}${num2}`;
}
return;
}
// Handle number buttons
if (operator === null) {
if (value === '.' && num1.includes('.')) {
return;
}
num1 += value;
expression.innerText = num1;
} else {
if (value === '.' && num2.includes('.')) {
return;
}
num2 += value;
expression.innerText = `${num1}${operator}${num2}`;
}
//Handle font size
if (expression.innerText.length > 15) {
display.style.fontSize = "20px";
} else if (display.innerText.length < 15) {
display.style.fontSize = "30px";
}
});
});