-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
170 lines (148 loc) · 3.7 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const display = document.querySelector(".display");
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
return a / b;
}
function operate(operator, a, b) {
a = +a;
b = +b;
let result;
switch (operator) {
case '+':
result = add(a, b);
break;
case '-':
result = subtract(a, b);
break;
case '*':
result = multiply(a, b);
break;
case '/':
if(b === 0){
return result = "Can't divide by 0";
} else {
result = divide(a, b);
break;
}
}
return Math.floor(result*1000)/1000;
}
let a = "";
let b = "";
let operator = "";
let operatorClicked = false;
let equalLocked = true;
// Add Event listeners to the NUMBER BUTTONS
// Save the first number in a
// After the operator was clicked save the number in b
let numbers = document.querySelectorAll(".number");
numbers.forEach(num => {
num.addEventListener("click", () => {
saveInput(num.textContent);
})
})
function saveInput(input){
console.log("click");
if (!operatorClicked) {
a += input;
display.textContent = a;
} else {
b += input;
display.textContent = b;
// If b was typed in we can unlock equals
equalLocked = false;
}
}
// When an operator is clicked save a and the OPERATOR BUTTONS
let operators = document.querySelectorAll(".operator");
operators.forEach(currentOperator => {
currentOperator.addEventListener("click", () => {
saveOperator(currentOperator.textContent);
});
})
function saveOperator(input){
if (!operatorClicked) {
operator = input;
operatorClicked = true;
} else {
// If the operator was clicked already it should function like equal
equals();
operator = input;
operatorClicked = true;
}
}
// When EQUAL clicked calculate with the appropriate method
let equal = document.querySelector(".equal");
equal.addEventListener("click", () => {
equals();
})
function equals() {
// Check if number b was typed in otherwise equal stays locked
if(!equalLocked){
// Save display value to b
b = display.textContent;
display.textContent = operate(operator, a, b);
init();
a = display.textContent;
}
}
// When CLEAR is clicked all is reset
let clear = document.querySelector(".clear");
clear.addEventListener("click", () => {
init();
display.textContent = "0";
})
function init() {
a = "";
b = "";
operator = "";
operatorClicked = false;
equalLocked = true;
}
// DOT button event handler
const dot = document.querySelector(".dot");
dot.addEventListener("click", () => {
if (!operatorClicked) {
if(!(a.includes("."))){
a += ".";
display.textContent = a;
}
} else {
if(!(b.includes("."))){
b += ".";
display.textContent = b;
}
}
})
// Add BACKSPACE event handler
const back = document.querySelector(".back");
back.addEventListener("click", () => {
if(!operatorClicked){
a = a.slice(0, a.length-1);
display.textContent = a;
} else {
b = b.slice(0, b.length - 1);
display.textContent = b;
}
})
// KEYBOARD support
document.addEventListener("keypress", (e) => {
if(/^[0-9]/.test(e.key)){
saveInput(e.key);
} else if(/^[-+*\/]/.test(e.key)){
saveOperator(e.key);
} else if(e.key === "Enter"){
equals();
} else if(e.key === "c"){
init();
display.textContent = "0";
}
})