-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
106 lines (87 loc) · 2.49 KB
/
app.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
// Create functions for adding, subtracting, multiplication, and dividing.
// Create a function operate that takes an operator(*, -, x, /) and 2 numbers and then calls one of the above functions.
function addition(x,y) {
calcValue = x + y;
updateDisplay()
calcValueX = ""
calcValueY = ""
operatorValue = ""
}
function subtraction(x,y) {
calcValue = x - y;
updateDisplay()
calcValueX = ""
calcValueY = ""
operatorValue = ""
}
function multiplication(x,y) {
calcValue = x * y;
updateDisplay()
calcValueX = ""
calcValueY = ""
operatorValue = ""
}
function division(x,y) {
calcValue = x / y;
updateDisplay()
calcValueX = ""
calcValueY = ""
operatorValue = ""
}
// Display
const displayOutput = document.querySelector('#output')
// Attempting with querySelectorAll
const numBtns = document.querySelectorAll('.numBtn')
const operatorBtns = document.querySelectorAll('.operatorBtn')
// Operator Buttons
const clearBtn = document.querySelector('#clear')
const equalsBtn = document.querySelector('#equals')
let calcValue = ""
let calcValueX = ""
let calcValueY = ""
let operatorValue = ""
numBtns.forEach((e) => {
e.addEventListener('click', (num) =>{
value = num.target.textContent
calcValue += value
updateDisplay()
})
})
operatorBtns.forEach((e) => {
e.addEventListener('click', (operator) => {
operatorValue = operator.target.textContent
if(calcValueX == ""){
calcValueX = calcValue;
calcValue = "";
updateDisplay()
}
})
})
function updateDisplay() {
displayOutput.innerText = calcValue
}
equalsBtn.addEventListener('click', ()=>{
if (calcValue != "" && calcValueX != ""){
calcValueY = calcValue;
calcValue = "";
updateDisplay()
if(calcValueX != "" && calcValueY != ""){
if(operatorValue == "+"){
addition(parseInt(calcValueX), parseInt(calcValueY))
} else if (operatorValue == "-"){
subtraction(parseInt(calcValueX), parseInt(calcValueY))
} else if (operatorValue == '*'){
multiplication(parseInt(calcValueX), parseInt(calcValueY))
} else if (operatorValue == "/"){
division(parseInt(calcValueX), parseInt(calcValueY))
}
}
}
})
clearBtn.addEventListener('click', ()=>{
calcValueX = ""
calcValueY = ""
operatorValue = ""
calcValue = ""
updateDisplay()
})