forked from LucioFex/Simple-Web-Calculator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculatorFunctionality.js
418 lines (339 loc) · 13.1 KB
/
calculatorFunctionality.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// JavaScript document - Simple Web Calculator - Luciano Esteban (2021)
// Fundamental constants & variables
const topScreen = document.getElementById("nums-top");
const bottomScreen = document.getElementById("nums-bottom");
const calculator = document.getElementById("calculator");
const calculatorInterface = document.getElementById("calculator-interface");
const calculatorScreen = document.getElementById("calculator-screen");
const skull = document.getElementById("skull")
const footer = document.getElementById("footer");
const colors = ["#294192", "#2f4d0d", "#790979", "#811414"];
const calcValues = {
num1: "1", num2: "2", num3: "3", num4: "4", num5: "5", num6: "6",
num7: "7", num8: "8", num9: "9", num0: "0", comma: ",", sum: "+",
substract: "-", divide: "÷", multiply: "x", factorial: "!", overX: "1/",
equalTo: "=", squareRoot: "√", cubeRoot: "∛", squarePower: "²",
pi: "3,1415926535897932384", euler: "2,7182818284590452353"};
const keyboardSymbols = {
"1": "num1", "2": "num2", "3": "num3", "4": "num4", "5": "num5",
"6": "num6", "7": "num7", "8": "num8", "9": "num9", "0": "num0",
"Backspace": "del1", "Delete": "clear", ",": "comma",
"+": "sum", "-": "substract", "/": "divide", "*": "multiply",
"Enter": "equalTo", "=": "equalTo", "e": "euler"
}
var givenResult = false;
var resultValue = "0";
var colorNum = 0;
var record = [];
function setUp() {
/*
Preparations for the usage of the calculator
*/
let terms = document.getElementById("terms");
let privacy = document.getElementById("privacy");
// Functionality of the terms and privacy buttons:
for (element of [terms, privacy]) {
element.addEventListener("click", helpSection, false);
}
// Update of the footer's width when the user opens the page
skullPosition()
// Change of the title background color in intervals of 4 seconds:
setInterval(multiColor, 1000 * 4, document.getElementById("title"));
// Functionality of the calculator buttons:
for (idName of document.getElementsByClassName("button")) {
buttonAction(idName);
}
// Link some symbols of the keyboard to the calculator
document.addEventListener("keydown", keyboardButtons, false);
}
function multiColor(title) {
/*
Change of the background color of the title of the page every 4 seconds
*/
if (colorNum > colors.length - 1) {colorNum = 0}
title.style.background = colors[colorNum];
colorNum += 1;
}
function buttonAction(input) {
/*
Button selection action:
It prepares all AddEventListeners() for the calculator buttons.
It divides the 'specialValues' from the normals.
*/
let specialValues = [
"equalTo", "overX", "factorial", "squareRoot", "cubeRoot",
"squarePower", "divide", "multiply", "sum", "substract"];
if (specialValues.includes(input.id)) {
input.addEventListener(
"click", () => processValue(input.id), false);
}
else if (specialValues.includes(input.id) === false) {
// If input.id is a number or not in the 'specialValues' list:
input.addEventListener(
"click", () => bottomScreenPrint(input.id), false);
}
}
function arithmeticSection(total, symbol, number) {
/*
The 'Total' parameter will be processed by 'number' with arithmetic symbols
*/
switch (symbol) {
case "+": return total + parseFloat(number);
case "-": return total - parseFloat(number);
case "x": return total * parseFloat(number);
case "÷": return total / parseFloat(number);
}
}
function scientificSection(symbol) {
/*
The 'Total' parameter will be processed by 'number' with scientific symbols
*/
let number = parseFloat(bottomScreen.innerHTML.replace(",", "."));
if (wrongInput(number, symbol)) {
bottomScreen.innerHTML = wrongInput(number, symbol);
return "error"
}
switch (symbol) {
case "1/": return 1 / number;
case "√": return Math.sqrt(number);
case "∛": return Math.cbrt(number);
case "²": return Math.pow(number, 2);
case "!":
if (number === 0) {return 1}
let inputNumber = number;
for (num=1; num < inputNumber; num++) {number *= num}
return number;
}
}
function calculateValues(history) {
/*
It process the history of values to send these numbers (depending
of their symbols) to diferents functions to return the result.
*/
if (Number.isNaN(parseFloat(history[0]))) {history.unshift("0")}
let result = parseFloat(history[0]);
for (value in history) {
let numberAndSymbol = [history[value -1], history[value]];
// Arithmetic section
if (["+", "-", "x", "÷"].includes(history[value - 1])) {
result = arithmeticSection(result, ...numberAndSymbol);
}
// Scientific section
else if (["1/", "!", "√", "∛", "²"].includes(history[value])) {
result = scientificSection(numberAndSymbol[1]);
}
}
if (result === "error") {record = [], resultValue = "0"}
return result;
}
function wrongInput(number, symbol) {
/*
Function that alerts if an input has an incorrect symbol.
*/
number = number.toString()
console.log(number);
let factorialError = symbol === "!" && number.includes("-", ".");
let rootError = symbol === "√" && number[0] === "-";
let zeroDivisionError = symbol === "1/" && number === undefined;
if (factorialError || rootError) {return "Invalid Input"}
else if (zeroDivisionError) {return "You can't divide by zero"}
}
function topScreenPrint(total) {
/*
Function to change the aspect of the top screen when a
the input is a scientific operator.
*/
let symbol = record.slice(-1)[0];
let preSymbol
let preTotal
let preProcess
if (record.length >= 4 && symbol) {
preTotal = calculateValues(record.slice(0, -2)).toString();
preSymbol = record.slice(-3)[0];
preProcess = `${preTotal} ${preSymbol}`;
}
else if (record.length < 4 && symbol !== "=") {preProcess = ""}
let screenNumber = bottomScreen.innerHTML;
switch (symbol) {
case "!":
case "²":
topScreen.innerHTML =
`${preProcess} (${screenNumber})${symbol}`;
break;
case "1/":
case "√":
case "∛":
topScreen.innerHTML =
`${preProcess} ${symbol}(${screenNumber})`;
break;
case "=":
topScreen.innerHTML += " =";
break
}
// Beginning of the givenResult mode
if (symbol !== "=" && record.length >= 4) {record = [preTotal, preSymbol]}
else if (symbol === "=" || record.length < 4) {record = []}
resultValue = total;
givenResult = true;
}
function screenModification(total) {
/*
Function that shows the output of the result in the bottom screen,
but will also show the process in the top one.
*/
topScreen.innerHTML = "";
total = total.toString();
if (total === "Infinity") {
bottomScreenPrint("clear");
return bottomScreen.innerHTML = "You can't divide by zero";
}
for (value in record) {
// If there's a scientific symbol
if (["1/", "!", "√", "∛", "²", "="].includes(record[value])) {
topScreenPrint(total);
break;
}
// If there's a simple number or an arithmetic symbol
topScreen.innerHTML += ` ${record[value].replace(".", ",")}`;
}
if (total !== "error") {bottomScreen.innerHTML = total.replace(".", ",")}
}
function processValue(sym) {
/*
Function that process the inserted symbol to print the
progress of the calculation's in the top screen and
the result of it in the bottom screen.
*/
if (resultValue.slice(-1) === ",") {resultValue = resultValue.slice(0, -1)}
record.push(resultValue.replace(",", "."), calcValues[sym]);
if (record.slice(-2)[0] === "0" && record.slice(-1)[0] !== "=" && record.length === 2) {
record = record.slice(0, -3);
record.push(calcValues[sym]);
}
// Visual process of the calculation in the top and bottom screen:
screenModification(calculateValues(record));
// Preparation for the next calculation
if (["1/", "!", "√", "∛", "²", "="].includes(calcValues[sym]) === false) {
resultValue = "0"}
skullPosition();
}
function givenResultCheck(sym) {
/*
Looks if the user got a result, after that checks if the
next input is a number or a symbol.
If the input is a number:
It restart the values of every screen.
If the input is a symbol:
It adds the last number in the top screen and then the operator.
*/
if (givenResult && sym !== "negate" && [0, 3].includes(record.length)) {
givenResult = false;
resultValue = "0";
record = [];
topScreen.innerHTML = "";
}
}
function bottomScreenPrint(sym) {
/*
Prints the selected number in the bottom screen,
including variables such as "pi" or "e". Even the 'negate'.
But if the input is a system calculator button such as
"clear", "ce" or "del", then it will delete characters.
*/
givenResultCheck(sym);
if (["clear", "ce"].includes(sym) || ["del1", "del2"].includes(sym)
&& resultValue.length === 1 || sym === "num0"
&& (resultValue === "" || resultValue === "0")) {
resultValue = "0";
}
if (sym === "clear") {
topScreen.innerHTML = "";
record = [];
}
else if (sym.includes("num") || sym === "comma"
&& resultValue.includes(",") === false) {
if (resultValue === "0" && sym !== "comma") {resultValue = ""}
resultValue += calcValues[sym];
}
else if (["pi", "euler"].includes(sym)) {
resultValue = calcValues[sym];
}
else if (sym === "negate" && resultValue !== "0") {
if (resultValue[0] !== "-") {resultValue = "-" + resultValue}
else if (resultValue[0] === "-") {resultValue = resultValue.slice(1)}
}
else if (["del1", "del2"].includes(sym) && resultValue.length > 1) {
resultValue = resultValue.slice(0, -1);
}
// Final print
bottomScreen.innerHTML = resultValue;
skullPosition();
}
function skullPosition() {
/*
This function changes the position of the skull
in the right of the calculator, including its image.
It also increases or reduces the width of the calculator
depending of the calculator's top and bottom screen length.
*/
let bottomWidth = (bottomScreen.innerHTML.length - 21) * 24;
let topWidth = (topScreen.innerHTML.length - 51) * 8;
let greaterWidth = topWidth;
if (bottomWidth >= topWidth) {greaterWidth = bottomWidth}
switch (greaterWidth <= 0) {
case true:
skull.style.left = "calc(50% + 300px)";
skull.src = "imgs/limit-screen.png";
calculator.style.width = "600px";
calculatorInterface.style.width = "560px";
calculatorScreen.style.width = "504px";
footer.style.width = "100%";
break;
case false:
skull.src = "imgs/over-limit-screen.png";
skull.style.left = `calc(50% + 300px + ${greaterWidth}px)`;
calculator.style.width = `calc(600px + ${greaterWidth}px)`;
calculatorInterface.style.width = `calc(560px + ${greaterWidth}px)`;
calculatorScreen.style.width = `calc(504px + ${greaterWidth}px)`;
footer.style.width = `calc(100% + ${greaterWidth}px)`;
break;
}
}
function keyboardButtons(event) {
/*
Function to use the numbers and some symbols of
the keyboard as calculator buttons.
*/
let bottomValues = Object.keys(keyboardSymbols).slice(0, 13);
let topValues = Object.keys(keyboardSymbols).slice(13);
if (bottomValues.includes(event.key)) {
bottomScreenPrint(keyboardSymbols[event.key]);
}
else if (topValues.includes(event.key)) {
processValue(keyboardSymbols[event.key]);
}
}
function helpSection(event) {
/*
Usage of the "terms and conditions" and "privacy policy" buttons.
*/
let id = event.target.id;
alert("I am supposed to show you something about " +
`${id[0].toUpperCase() + id.slice(1)} and stuff like that.`);
if (id === "terms") {
alert("The license of this web site is the MIT LICENSE.");
}
else if (id === "privacy") {
privacyTexts = [
"Knowing that this web site is about a calculator, there's " +
"no need to set any kind of privacy politic. Relax...",
"But I wanna learn how to redirect you from this page to another" +
" one, so... Let's look at the formal definition of privacy :)",
"Oh, I almost forget: \nIf you'd have read this, then surely your" +
" navigator will cancel the next page that I will try to open." +
"\n\nIf you skipped everything, then you will have no problem >:("]
for (text of privacyTexts) {alert(text)}
window.open("https://en.wikipedia.org/wiki/Privacy", "_blank");
}
}
window.addEventListener('load', setUp, false); // Starts the script