Skip to content

Commit 3e5a790

Browse files
committed
Add functiaonalities
1 parent 4995b96 commit 3e5a790

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

Source-Code/Calculator/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</head>
99
<body>
1010
<div class="calculator">
11-
<input type="text" class="calculator-screen" value="" disabled />
11+
<input type="text" class="calculator-screen" value="" disabled placeholder="0" />
1212
<div class="calculator-keys">
1313

1414
<button type="button" value="7">7</button>

Source-Code/Calculator/script.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
let firstOperand = "";
2+
let secondOperand = "";
3+
let currentOperator = null;
4+
let awaitingSecondOperand = false;
5+
6+
const display = document.querySelector(".calculator-screen");
7+
const keys = document.querySelector(".calculator-keys");
8+
9+
const updateDisplay = () => {
10+
display.value = awaitingSecondOperand ? secondOperand : firstOperand;
11+
};
12+
13+
const resetCalculator = () => {
14+
firstOperand = "";
15+
secondOperand = "";
16+
currentOperator = null;
17+
awaitingSecondOperand = false;
18+
updateDisplay();
19+
};
20+
21+
const inputNumber = (number) => {
22+
if (awaitingSecondOperand) {
23+
secondOperand += number;
24+
} else {
25+
firstOperand += number;
26+
}
27+
updateDisplay();
28+
};
29+
30+
const inputOperator = (operator) => {
31+
if (!firstOperand) return;
32+
33+
if (secondOperand) {
34+
calculate();
35+
}
36+
37+
currentOperator = operator;
38+
awaitingSecondOperand = true;
39+
};
40+
41+
const inputDecimal = () => {
42+
if (awaitingSecondOperand) {
43+
if (!secondOperand.includes(".")) {
44+
secondOperand += ".";
45+
}
46+
} else {
47+
if (!firstOperand.includes(".")) {
48+
firstOperand += ".";
49+
}
50+
}
51+
updateDisplay();
52+
};
53+
54+
const calculate = () => {
55+
let result;
56+
const first = parseFloat(firstOperand);
57+
const second = parseFloat(secondOperand);
58+
59+
if (Number.isNaN(first) || Number.isNaN(second)) return;
60+
61+
switch (currentOperator) {
62+
case "+":
63+
result = first + second;
64+
break;
65+
case "-":
66+
result = first - second;
67+
break;
68+
case "*":
69+
result = first * second;
70+
break;
71+
case "/":
72+
result = first / second;
73+
break;
74+
default:
75+
return;
76+
}
77+
78+
firstOperand = String(result);
79+
secondOperand = "";
80+
awaitingSecondOperand = false;
81+
currentOperator = null;
82+
updateDisplay();
83+
};
84+
85+
keys.addEventListener("click", (event) => {
86+
const { target } = event;
87+
const { value } = target;
88+
89+
if (!target.matches("button")) return;
90+
91+
switch (value) {
92+
case "all-clear":
93+
resetCalculator();
94+
break;
95+
case "=":
96+
calculate();
97+
break;
98+
case ".":
99+
inputDecimal();
100+
break;
101+
case "+":
102+
case "-":
103+
case "*":
104+
case "/":
105+
inputOperator(value);
106+
break;
107+
default:
108+
if (Number.isInteger(parseFloat(value))) {
109+
inputNumber(value);
110+
}
111+
}
112+
});
113+
114+
document.addEventListener("DOMContentLoaded", updateDisplay);

0 commit comments

Comments
 (0)