-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.html
196 lines (192 loc) · 4.86 KB
/
calculator.html
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Calculator Using Java Script</title>
</head>
<style>
body {
background-color: lightblack;
}
.calculator {
text-align: center;
margin: 0 auto;
padding: 200px;
}
.calculator-screen {
width: 100%;
height: 100px;
background-color:#b6f9ff;
color:black;
text-align: right;
font-size: 36px;
border: 5px solid black;
padding: 0 20px;
box-sizing: border-box;
}
.calculator-keys {
width: 100%;
}
.row {
display: flex;
}
button {
height: 80px;
background-color: gray;
border: 5px solid black;
font-size: 32px;
color: #fff;
width: 25%;
outline: none;
}
.all-clear,
.equals {
width: 50%;
}
.operator,
.equals {
background-color: orange;
}
button:hover {
background-color: darkgray;
}
.operator:hover,
.equals:hover {
background-color: darkorange;
}
</style>
<body>
<div class="calculator">
<input type="text" class="calculator-screen" value="0" disabled />
<div class="calculator-keys">
<div class="row">
<button class="all-clear">AC</button>
<button class="percent">%</button>
<button class="operator" value="/">÷</button>
</div>
<div class="row">
<button class="number" value="7">7</button>
<button class="number" value="8">8</button>
<button class="number" value="9">9</button>
<button class="operator" value="*">×</button>
</div>
<div class="row">
<button class="number" value="4">4</button>
<button class="number" value="5">5</button>
<button class="number" value="6">6</button>
<button class="operator" value="-">−</button>
</div>
<div class="row">
<button class="number" value="1">1</button>
<button class="number" value="2">2</button>
<button class="number" value="3">3</button>
<button class="operator" value="+">+</button>
</div>
<div class="row">
<button class="number zero-btn" value="0">0</button>
<button class="decimal" value=".">˙</button>
<button class="equals">=</button>
</div>
</div>
</div>
<script>
let prevNumber = "";
let calculationOperator = "";
let currentNumber = "0";
let multioperator = 0;
const calculate = () => {
let result = "";
switch (calculationOperator) {
case "+":
result = parseFloat(prevNumber) + parseFloat(currentNumber);
break;
case "-":
result = parseFloat(prevNumber) - parseFloat(currentNumber);
break;
case "*":
result = parseFloat(prevNumber) * parseFloat(currentNumber);
break;
case "/":
result = parseFloat(prevNumber) / parseInt(currentNumber);
default:
break;
}
currentNumber = result;
calculationOperator = "";
};
const percantage = document.querySelector(".percent");
percantage.addEventListener("click", () => {
if (prevNumber != "") {
currentNumber = prevNumber / 100;
} else {
currentNumber /= 100;
}
updateScreen(currentNumber);
});
const calculatorScreen = document.querySelector(".calculator-screen");
const updateScreen = (number) => {
calculatorScreen.value = number;
};
const inputNumber = (number) => {
if (currentNumber === "0") {
currentNumber = number;
return;
}
currentNumber += number;
console.log(currentNumber);
};
const equalSign = document.querySelector(".equals");
equalSign.addEventListener("click", (event) => {
console.log("Update " + currentNumber);
calculate();
updateScreen(currentNumber);
});
const inputOperator = (operator) => {
if (calculationOperator === "") {
prevNumber = currentNumber;
}
calculationOperator = operator;
console.log(`Multi Operator ${multioperator}`);
currentNumber = "0";
};
// operator
const operators = document.querySelectorAll(".operator");
operators.forEach((operator) => {
operator.addEventListener("click", (event) => {
inputOperator(event.target.value);
console.log(event.target.value);
});
});
inputDecimal = (dot) => {
if (currentNumber.includes(dot)) {
return;
}
currentNumber += dot;
};
const decimalNumber = document.querySelector(".decimal");
decimalNumber.addEventListener("click", (event) => {
inputDecimal(event.target.value);
updateScreen(currentNumber);
});
const clearAll = () => {
prevNumber = "";
calculationOperator = "";
currentNumber = "0";
multioperator = 0;
};
const clearButton = document.querySelector(".all-clear");
clearButton.addEventListener("click", (event) => {
clearAll();
updateScreen(currentNumber);
});
const numbers = document.querySelectorAll(".number");
numbers.forEach((number) => {
number.addEventListener("click", (event) => {
inputNumber(event.target.value);
updateScreen(currentNumber);
});
});
</script>
</body>
</html>