-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
40 lines (38 loc) · 1.42 KB
/
scripts.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
const display = document.getElementById('display');
const buttons = document.querySelectorAll('button');
let currentInput = "";
let operation = "";
let firstValue = "";
buttons.forEach(button => {
button.addEventListener('click', function() {
const value = this.innerText;
if(!isNaN(value) || value === ".") {
currentInput += value;
display.innerText = currentInput;
} else if (value === "+" || value === "-" || value === "*" || value === "/") {
operation = value;
firstValue = currentInput;
currentInput = "";
} else if (value === "=") {
switch(operation) {
case "+":
currentInput = parseFloat(firstValue) + parseFloat(currentInput);
break;
case "-":
currentInput = parseFloat(firstValue) - parseFloat(currentInput);
break;
case "*":
currentInput = parseFloat(firstValue) * parseFloat(currentInput);
break;
case "/":
currentInput = parseFloat(firstValue) / parseFloat(currentInput);
break;
default:
break;
}
display.innerText = currentInput;
operation = "";
firstValue = "";
}
});
});