-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
120 lines (106 loc) · 3.04 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
function add (num1, num2) {
return num1 + num2;
}
function subtract (num1, num2) {
return num1 - num2;
}
function multiply (num1, num2) {
return num1 * num2;
}
function divide (num1, num2) {
if (num2 === 0) return "ERROR";
return num1 / num2;
}
let num1 = ''
let num2 = ''
let operator = null
let result = null
function operate (operator, num1, num2) {
num1 = parseFloat(num1)
num2 = parseFloat(num2)
if (operator == '+') {
return add(num1, num2)
}
else if (operator == '-') {
return subtract(num1, num2)
}
else if (operator == '*') {
return multiply(num1, num2)
}
else if (operator == '/') {
return divide(num1, num2)
}
else return null
}
const lowerDisplay = document.querySelector('#lower');
const upperDisplay = document.querySelector('#upper')
const numbers = document.querySelectorAll('.digit')
const operators = document.querySelectorAll('.operator');
const clearButton = document.querySelector('#ac');
const signButton = document.querySelector('#sign');
const percentButton = document.querySelector('#percent');
const equalsButton = document.querySelector('#equals');
clearButton.addEventListener('click', () => {
lowerDisplay.textContent = ''
upperDisplay.textContent = ''
num1 = ''
num2 = ''
operator = null
})
// function updateDisplay() {
// upperDisplay.textContent = `${num1} ${operator}`
// lowerDisplay.textContent = num2
// }
numbers.forEach(button => {
button.addEventListener('click', () => {
if (result !== null) {
lowerDisplay.textContent = ''
result = null
}
if (lowerDisplay.textContent.includes('.') && button.textContent === '.') return
lowerDisplay.textContent += button.textContent
})
})
operators.forEach(button => {
button.addEventListener('click', () => {
if (num1 !== '') {
num2 = lowerDisplay.textContent
if (num2 == '') {
return
}
result = operate(operator, num1, num2)
num1 = result
}
// else if (num1 !== '' && num2 == '') return
else {
num1 = lowerDisplay.textContent
}
operator = button.textContent
lowerDisplay.textContent = ''
if (num1 == '') return
else upperDisplay.textContent = `${num1} ${operator}`
})
})
equalsButton.addEventListener('click', () => {
num2 = lowerDisplay.textContent
result = operate(operator, num1, num2)
if (result == null) return
else {
lowerDisplay.textContent = result
upperDisplay.textContent = ''
num1 = ''
num2 = ''
operator = null
result = null
}
})
signButton.addEventListener('click', () => {
if (lowerDisplay.textContent) {
lowerDisplay.textContent = String(-parseFloat(lowerDisplay.textContent))
}
})
percentButton.addEventListener('click', () => {
if (lowerDisplay.textContent) {
lowerDisplay.textContent = String(parseFloat(lowerDisplay.textContent) / 100)
}
})