-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
358 lines (306 loc) · 8.7 KB
/
Copy pathscript.js
File metadata and controls
358 lines (306 loc) · 8.7 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Caclulator App Script
// Set up screen and current number tracking
const screenValue = document.querySelector("#screen");
let args = ["", "", ""];
let currentArg = 0;
// Render calculations to screen
let widthCount = 0;
const MAXWIDTH = 12;
// Array for checking if the button was a number
const numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
// Function for Number Input
function addValue(arg) {
if (arg === "0" && args[currentArg] === "0") {
return;
} else if (numbers.includes(arg) && args[currentArg].length < 5) {
args[currentArg] = args[currentArg] + arg;
}
}
// Addition Function
function calcAdd() {
let num1 = parseFloat(args[0]);
let num2 = parseFloat(args[2]);
let result = (num1 + num2).toFixed(2);
return Number(result);
}
// Subtraction Function
function calcSubtract() {
let num1 = parseFloat(args[0]);
let num2 = parseFloat(args[2]);
let result = (num1 - num2).toFixed(2);
return Number(result);
}
// Multiplication Function
function calcMultiply() {
let num1 = parseFloat(args[0]);
let num2 = parseFloat(args[2]);
let result = (num1 * num2).toFixed(2);
return Number(result);
}
// Division Function
function calcDivide() {
let num1 = parseFloat(args[0]);
let num2 = parseFloat(args[2]);
let result = (num1 / num2).toFixed(2);
return Number(result);
}
function renderScreen() {
if (args[1] === "") {
widthCount = args[0].length;
screenValue.value = `${args[0]}`;
} else {
widthCount = args[0].length + args[1].length + args[2].length + 1;
screenValue.value = `${args[0]} ${args[1]} ${args[2]}`;
}
}
// Initialize all the buttons on the numpad
const Button = {
zero: document.querySelector("#zero"),
one: document.querySelector("#one"),
two: document.querySelector("#two"),
three: document.querySelector("#three"),
four: document.querySelector("#four"),
five: document.querySelector("#five"),
six: document.querySelector("#six"),
seven: document.querySelector("#seven"),
eight: document.querySelector("#eight"),
nine: document.querySelector("#nine"),
add: document.querySelector("#add"),
subtract: document.querySelector("#subtract"),
multiply: document.querySelector("#multiply"),
divide: document.querySelector("#divide"),
one: document.querySelector("#one"),
period: document.querySelector("#period"),
enter: document.querySelector("#enter"),
clear: document.querySelector("#clear"),
backspace: document.querySelector("#backspace"),
mystery1: document.querySelector("#mystery1"),
mystery2: document.querySelector("#mystery2"),
};
Button.zero.addEventListener("click", function (e) {
addValue("0");
renderScreen();
});
Button.one.addEventListener("click", function (e) {
addValue("1");
renderScreen();
});
Button.two.addEventListener("click", function (e) {
addValue("2");
renderScreen();
});
Button.three.addEventListener("click", function (e) {
addValue("3");
renderScreen();
});
Button.four.addEventListener("click", function (e) {
addValue("4");
renderScreen();
});
Button.five.addEventListener("click", function (e) {
addValue("5");
renderScreen();
});
Button.six.addEventListener("click", function (e) {
addValue("6");
renderScreen();
});
Button.seven.addEventListener("click", function (e) {
addValue("7");
renderScreen();
});
Button.eight.addEventListener("click", function (e) {
addValue("8");
renderScreen();
});
Button.nine.addEventListener("click", function (e) {
addValue("9");
renderScreen();
});
Button.add.addEventListener("click", function (e) {
if (args[0] === "") {
return;
} else if (currentArg === 0) {
args[currentArg + 1] = `+`;
currentArg += 2;
} else if (currentArg === 2) {
calculate();
args[currentArg + 1] = `+`;
currentArg += 2;
} else {
return;
}
renderScreen();
});
Button.subtract.addEventListener("click", function (e) {
if (args[0] === "") {
return;
} else if (currentArg === 0) {
args[currentArg + 1] = `-`;
currentArg += 2;
} else if (currentArg === 2) {
calculate();
args[currentArg + 1] = `-`;
currentArg += 2;
} else {
return;
}
renderScreen();
});
Button.multiply.addEventListener("click", function (e) {
if (args[0] === "") {
return;
} else if (currentArg === 0) {
args[currentArg + 1] = `*`;
currentArg += 2;
} else if (currentArg === 2) {
calculate();
args[currentArg + 1] = `*`;
currentArg += 2;
} else {
return;
}
renderScreen();
});
Button.divide.addEventListener("click", function (e) {
if (args[0] === "") {
return;
} else if (currentArg === 0) {
args[currentArg + 1] = `/`;
currentArg += 2;
} else if (currentArg === 2) {
calculate();
args[currentArg + 1] = `/`;
currentArg += 2;
} else {
return;
}
renderScreen();
});
Button.clear.addEventListener("click", function (e) {
args = ["", "", ""];
currentArg = 0;
renderScreen();
});
Button.enter.addEventListener("click", function (e) {
calculate();
});
function calculate() {
if (args[2] === "") {
return;
} else if (args[1] === "+") {
args[0] = String(calcAdd());
args[1] = "";
args[2] = "";
currentArg = 0;
renderScreen();
} else if (args[1] === "-") {
args[0] = String(calcSubtract());
args[1] = "";
args[2] = "";
currentArg = 0;
renderScreen();
} else if (args[1] === "*") {
args[0] = String(calcMultiply());
args[1] = "";
args[2] = "";
currentArg = 0;
renderScreen();
} else if (args[1] === "/") {
args[0] = String(calcDivide());
args[1] = "";
args[2] = "";
currentArg = 0;
renderScreen();
}
}
Button.backspace.addEventListener("click", function (e) {
if (args[0].length <= 0) {
return;
} else if (currentArg === 2 && args[currentArg] === "") {
args[1] = "";
currentArg = 0;
renderScreen();
} else {
args[currentArg] = args[currentArg].slice(0, args[currentArg].length - 1);
renderScreen();
}
});
Button.period.addEventListener("click", function (e) {
if (args[currentArg] === "") {
return;
} else if (args[currentArg].includes(".") === false) {
args[currentArg] += ".";
renderScreen();
}
});
Button.mystery1.addEventListener("click", function (e) {
randomizeAll();
});
// Function for randomizing hex codes
function randomHex() {
hexCode = "#";
for (let i = 0; i < 6; i++) {
// Random number from 0-15
let randomNum = Math.round(Math.random() * 100) % 16;
if (randomNum > 9) {
if (randomNum === 10) {
randomNum = "A";
} else if (randomNum === 11) {
randomNum = "B";
} else if (randomNum === 12) {
randomNum = "C";
} else if (randomNum === 13) {
randomNum = "D";
} else if (randomNum === 14) {
randomNum = "E";
} else if (randomNum === 15) {
randomNum = "F";
}
}
hexCode = hexCode + randomNum;
}
return hexCode;
}
function randomizeAll() {
Button.clear.style.backgroundColor = randomHex();
Button.clear.style.color = randomHex();
Button.mystery1.style.backgroundColor = randomHex();
Button.mystery1.style.color = randomHex();
Button.mystery2.style.backgroundColor = randomHex();
Button.mystery2.style.color = randomHex();
Button.backspace.style.backgroundColor = randomHex();
Button.backspace.style.color = randomHex();
Button.seven.style.backgroundColor = randomHex();
Button.seven.style.color = randomHex();
Button.eight.style.backgroundColor = randomHex();
Button.eight.style.color = randomHex();
Button.nine.style.backgroundColor = randomHex();
Button.nine.style.color = randomHex();
Button.add.style.backgroundColor = randomHex();
Button.add.style.color = randomHex();
Button.subtract.style.backgroundColor = randomHex();
Button.subtract.style.color = randomHex();
Button.four.style.backgroundColor = randomHex();
Button.four.style.color = randomHex();
Button.five.style.backgroundColor = randomHex();
Button.five.style.color = randomHex();
Button.six.style.backgroundColor = randomHex();
Button.six.style.color = randomHex();
Button.one.style.backgroundColor = randomHex();
Button.one.style.color = randomHex();
Button.two.style.backgroundColor = randomHex();
Button.two.style.color = randomHex();
Button.three.style.backgroundColor = randomHex();
Button.three.style.color = randomHex();
Button.multiply.style.backgroundColor = randomHex();
Button.multiply.style.color = randomHex();
Button.period.style.backgroundColor = randomHex();
Button.period.style.color = randomHex();
Button.zero.style.backgroundColor = randomHex();
Button.zero.style.color = randomHex();
Button.enter.style.backgroundColor = randomHex();
Button.enter.style.color = randomHex();
Button.divide.style.backgroundColor = randomHex();
Button.divide.style.color = randomHex();
}