-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
123 lines (105 loc) · 3.03 KB
/
script.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
111
112
113
114
115
116
117
118
119
120
121
122
// Select DOM elements
const display = document.getElementById('display');
const buttons = document.querySelectorAll('.button');
// Initialize variables
let currentInput = '';
let operator = null;
let previousInput = '';
// Function to update the display
function updateDisplay() {
if (currentInput === '' && previousInput === '') {
display.textContent = '0';
} else if (operator && currentInput === '') {
display.textContent = `${previousInput} ${operator}`;
} else if (operator) {
display.textContent = `${previousInput} ${operator} ${currentInput}`;
} else {
display.textContent = currentInput;
}
}
// Function to handle number and decimal input
function handleNumber(number) {
if (number === '.' && currentInput.includes('.')) return; // Prevent multiple decimals
if (currentInput.length >= 12) return; // Limit input length
currentInput += number;
updateDisplay();
}
// Function to handle operator input
function handleOperator(op) {
if (currentInput === '' && previousInput === '') return; // Do nothing if no input
if (operator !== null && currentInput === '') {
// Change operator if no new number is entered
operator = op;
updateDisplay();
return;
}
if (operator !== null) {
calculate();
}
operator = op;
previousInput = currentInput || previousInput;
currentInput = '';
updateDisplay();
}
// Function to calculate the result
function calculate() {
let result;
const prev = parseFloat(previousInput);
const current = parseFloat(currentInput);
if (isNaN(prev) || isNaN(current)) return;
switch (operator) {
case '+':
result = prev + current;
break;
case '-':
result = prev - current;
break;
case '*':
result = prev * current;
break;
case '/':
if (current === 0) {
alert("Cannot divide by zero");
resetCalculator();
return;
}
result = prev / current;
break;
default:
return;
}
currentInput = result.toString();
operator = null;
previousInput = '';
updateDisplay();
}
// Function to reset the calculator
function resetCalculator() {
currentInput = '';
operator = null;
previousInput = '';
updateDisplay();
}
// Add event listeners to all buttons
buttons.forEach(button => {
button.addEventListener('click', () => {
const number = button.dataset.number;
const op = button.dataset.operator;
const isClear = button.classList.contains('clear');
const isEquals = button.classList.contains('equals');
if (number !== undefined) {
handleNumber(number);
}
if (op !== undefined) {
handleOperator(op);
}
if (isClear) {
resetCalculator();
}
if (isEquals) {
calculate();
}
});
});
// Initialize the display
updateDisplay();