-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
200 lines (163 loc) · 5.38 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const displayElement = document.querySelector('.displayNumber');
const numberButtons = document.querySelectorAll('.numbers');
const operatorButtons = document.querySelectorAll('.operator');
const clearButton = document.querySelector("#clear");
const decimalButton = document.querySelector("#decimal");
const backSpaceButton = document.querySelector("#backspace");
let displayValue = "";
let firstOperand = "";
let storedOperation = "";
let secondOperand = "";
let result = 0;
backSpaceButton.addEventListener('pointerdown', deleteOneCharacter);
decimalButton.addEventListener('pointerdown', addDecimal);
clearButton.addEventListener('pointerdown', clearAllContent);
operatorButtons.forEach((operator) => {
operator.addEventListener('pointerdown', storingValueAndOperation);
})
numberButtons.forEach((button) => {
button.addEventListener('pointerdown', displayValueToScreen);
})
// equalButton.addEventListener('click', EqualButtonClicked);
document.addEventListener('keydown', function (e) {
if (e.key === '1' || e.key === '2' || e.key === '3' || e.key === '4' || e.key === '5' || e.key === '6' || e.key === '7' || e.key === '8' || e.key === '9' || e.key === '0') {
displayValueToScreen(e)
}else if(e.key === '*' || e.key === '-' || e.key === '/' || e.key === "+" || e.key === "="){
storingValueAndOperation(e);
}else if(e.key === 'Backspace'){
deleteOneCharacter(e);
}else if(e.key === "c"){
clearAllContent(e);
}else if(e.key === '.'){
addDecimal(e);
}
})
// function displayValueToScreen(event) {
// displayValue += event.key;
// displayElement.textContent = displayValue;
//
function displayValueToScreen(event){
let number;
if(event.key){
number = event.key;
displayValue += number;
displayElement.textContent = displayValue;
}else if(event.currentTarget){
number = event.currentTarget.textContent;
displayValue += number;
displayElement.textContent = displayValue;
}
}
function addDecimal(event) {
result = displayElement.textContent;
let decimal;
if(event.key){
decimal = event.key;
if (result.indexOf(decimal) === -1) {
displayValueToScreen(event);
return;
} else {
return;
}
}
else if(event.currentTarget.textContent){
decimal = event.currentTarget.textContent;
if (result.indexOf(decimal) === -1) {
displayValueToScreen(event);
return;
} else {
return;
}
}
}
function deleteOneCharacter(event) {
result = displayElement.textContent;
console.log(typeof result);
let deleteOneCharacter = event.currentTarget;
console.log(typeof displayElement.textContent);
if (deleteOneCharacter) {
console.log("YOU PRESSED ME!");
result = result.slice(0, result.length - 1);
displayElement.textContent = result;
displayValue = result;
} else {
return;
}
}
/*
When user clicks upon backspace, it should delete one character from string, if we're
targeting the displayELement.textContent
*/
function clearAllContent() {
console.log("You're pressing me!");
displayValue = "";
firstOperand = "";
secondOperand = "";
result = 0;
storedOperation = "";
displayElement.textContent = ""
currentOperation = "";
}
function storingValueAndOperation(event) {
const currentOperation = event.key || event.currentTarget.textContent;
if(storedOperation === "" && currentOperation != "=") {
firstOperand = displayValue;
storedOperation = currentOperation;
firstOperand = displayValue;
displayValue = "";
} else if (currentOperation === "=" && firstOperand != "" && storedOperation != "") {
secondOperand = displayValue;
if (secondOperand === "0" && storedOperation === "/") {
alert("You Cannot divide by 0");
clearAllContent();
return;
}
result = parseFloat(operate(+firstOperand, storedOperation, +secondOperand).toFixed(2));
displayValue = result;
displayElement.textContent = displayValue;
firstOperand = displayValue;
secondOperand = "";
storedOperation = "";
} else if (currentOperation != "=") {
secondOperand = displayValue;
result = parseFloat(operate(+firstOperand, storedOperation, +secondOperand).toFixed(2));
firstOperand = result;
displayElement.textContent = firstOperand;
storedOperation = currentOperation;
displayValue = "";
}
}
function addNumbers(Num1, Num2) {
let result = Num1 + Num2;
return result;
}
function subtractNumbers(Num1, Num2) {
let result = Num1 - Num2;
return result;
}
function multiplyNumbers(Num1, Num2) {
let result = Num1 * Num2;
return result;
}
function divideNumbers(Num1, Num2) {
let result = Num1 / Num2;
return result;
}
function operate(Num1, operator, Num2) {
switch (operator) {
case '+':
return addNumbers(Num1, Num2);
break;
case '-':
return subtractNumbers(Num1, Num2);
break;
case '*':
return multiplyNumbers(Num1, Num2);
break;
case '/':
return divideNumbers(Num1, Num2);
break;
default:
alert("No Valid Input");
}
}