-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
395 lines (313 loc) · 8.2 KB
/
calculator.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
// Color NPM Packages
const prompt = require("prompt-sync")({ sigint: true });
const colors = require("colors");
// const chalk = require("chalk");
const chalkAnimation = require("chalk-animation");
// END: Color NPM packages
// Variables declared
let welcome;
let menu;
let xValue;
let yValue;
let width;
// END: Variables declared
// Functions
const randomUpTo = function (max) {
randomNum = Math.floor(Math.random() * (max + 1));
return Number(randomNum);
};
const validation1 = function (menuChoice) {
while (
menuChoice < 0 ||
menuChoice > 10 ||
isNaN(menuChoice) ||
menuChoice == ""
) {
console.log("");
console.log("Not a valid number ".white.bgRed);
console.log("");
menuChoice = Number(
prompt("Choose a menu number (0-10) and press enter: ".black.bgYellow)
);
}
return Number(menuChoice);
};
const validation2 = function (numNotStr) {
if (numNotStr == "r") {
return randomUpTo(100);
}
while (isNaN(numNotStr) & (numNotStr != "r")) {
console.log("");
console.log("Not a valid number ".white.bgRed);
console.log("");
numNotStr = prompt(
`"${numNotStr}" is not valid. Type a NUMBER: `.green.bold
);
}
if (numNotStr == "r") {
return randomUpTo(100);
}
return numNotStr;
};
// END: Functions validation
// WELCOME
console.clear();
console.log(
'\n\n 😀 Welcome to the "RAINBOW CALCULATOR" \n\n\n\n'
.rainbow
);
welcome = prompt("Please enter your name here: ".black.bgWhite);
console.clear();
while (welcome == "" || parseFloat(welcome) || welcome.length < 3) {
console.log("\nCome on, tell me your name man!\n".green);
welcome = prompt("Please enter your name here: ".black.bgWhite);
}
// END: WELCOME
// MENU
console.clear();
console.log(
`\nHello ${welcome.italic.underline.bold}! What you wanna do with our calculator?`
.yellow
);
console.log(
`\n
0 - Calculate Number PI
1 - Calculate Eulner's number
2 - Calculate Ratio (x,y, width)
3 - Calculate Percentage (x,y)
4 - calculate.add(x, y)
5 - calculate.subtract(x, y)
6 - calculate.multiply(x, y)
7 - calculate.divide(x, y)
8 - calculate.modulation(x, y)
9 - calculate.elevate(x, y)
10 - calculate.sqrt(x)\n`.italic.cyan
);
menu = prompt(" Choose a menu number(0-10) and press enter: ".black.bgYellow);
menu = validation1(menu);
if (Number(menu) == 2) {
console.clear();
console.log(
'\n This function calculates the "height" considering a given "width" an a given ratio "x (width) : y (height)". Choose next your variables: '
.italic
);
} else if (Number(menu) == 3) {
console.clear();
console.log(
'\n This function calculates the percentage of "x" in "y". Choose next your variables:'
.italic
);
} else if (Number(menu) == 4) {
console.clear();
console.log(
'\n This function calculates the simply sum of "x" + "y". Choose next your variables:'
.italic
);
} else if (Number(menu) == 5) {
console.clear();
console.log(
'\n This function calculates the simply subtraction of "x" - "y". Choose next your variables:'
.italic
);
} else if (Number(menu) == 6) {
console.clear();
console.log(
'\n This function calculates the multiplication "x" * "y". Choose next your variables:'
.italic
);
} else if (Number(menu) == 7) {
console.clear();
console.log(
'\n This function calculates the division of "x" / "y". Choose next your variables:'
.italic
);
} else if (Number(menu) == 8) {
console.clear();
console.log(
'\n This function returns the remainder of "x" divided by "y". Choose next your variables:'
.italic
);
} else if (Number(menu) == 9) {
console.clear();
console.log(
'\n This function returns the power of "x" elevated to "y". Choose next your variables:'
.italic
);
} else if (Number(menu) == 10) {
console.clear();
console.log(
'\n This function returns the square root of "x". Choose next your variables:'
.italic
);
}
// END: MENU
// VALUES INPUT
// first Value: X
if (
Number(menu) == 2 ||
Number(menu) == 3 ||
Number(menu) == 4 ||
Number(menu) == 5 ||
Number(menu) == 6 ||
Number(menu) == 7 ||
Number(menu) == 8 ||
Number(menu) == 9 ||
Number(menu) == 10
) {
xValue = prompt(
'Choose a NUMBER as your "x" variable (type "r" for random): '.green.bold
);
xValue = validation2(xValue);
console.log(`\nNumber chosen was: ${xValue}\n\n`.magenta);
}
// second Value: Y
{
if ((Number(menu) >= 2) & (Number(menu) < 10)) {
yValue = prompt(
'Choose a NUMBER as your "y" variable (type "r" for random): '.green.bold
);
yValue = validation2(yValue);
console.log(`\nNumber chosen was: ${yValue}\n\n`.magenta);
}
}
// third Value: width
{
if (Number(menu) == 2) {
width = prompt(
'Choose a NUMBER as your "width" variable (type "r" for random): '.green
.bold
);
width = validation2(width);
console.log(`\nNumber chosen was: ${width}\n\n`.magenta);
}
}
// END: VALUES INPUT
console.clear();
pause = prompt("Great! Press now enter to get your result!".bold);
// Class Declaration
class Calculator {
constructor(x, y, width) {
this.x = x;
this.y = y;
this.width = width;
this.pi = 3.141592653589793;
this.e = 2.718281828459045;
}
// Class Methods
ratio() {
let ratio = Math.abs(this.y) / Math.abs(this.x);
return ratio * this.width;
}
percentage() {
return `${(this.x / this.y) * 100}%`;
}
add() {
return +this.x + +this.y;
}
subtract() {
return this.x - this.y;
}
multiply() {
return this.x * this.y;
}
divide() {
if (this.y == 0) {
console.log('\nERROR: the divisor cannot be "0"'.red);
} else {
return this.x % this.y;
}
}
modulation() {
if (this.y == 0) {
console.log('\nERROR: the divisor cannot be "0"'.red);
} else {
return this.x % this.y;
}
}
elevate() {
return Math.pow(this.x, this.y);
}
sqrt() {
return Math.sqrt(this.x);
}
// END - Class Methods and Class Declaration
}
// Class Instance
const calculate = new Calculator(xValue, yValue, width);
// Returns PI (3.141592653589793)
if (Number(menu) == 0) {
console.log(`\nPI = ${calculate.pi}\n`.rainbow);
}
// Returns Eulner's number (2.718281828459045)
if (Number(menu) == 1) {
console.log(`\nEulner's number = ${calculate.e}\n`.rainbow);
}
//return height is --- on ratio x:y
if (Number(menu) == 2) {
console.log(`\nHeigh = ${calculate.ratio()}\n`.rainbow);
}
// return percentage of x in y.
if (Number(menu) == 3) {
console.log(
`\n"x"(${xValue}) = ${calculate.percentage()} of "y"(${yValue})\n`.rainbow
);
}
// Returns the sum of x added to y.
if (Number(menu) == 4) {
console.log(
`\n"x"(${xValue}) + "y"(${yValue}) = ${calculate.add()}\n`.rainbow
);
}
// Returns the difference of y subtracted from x.
if (Number(menu) == 5) {
console.log(
`\n"x"(${xValue}) - "y"(${yValue}) = ${calculate.subtract()}\n`.rainbow
);
}
// Returns the product of x multiplied by y.
if (Number(menu) == 6) {
console.log(
`\n"x"(${xValue}) * "y"(${yValue}) = ${calculate.multiply()}\n`.rainbow
);
}
// Returns the quotient of x divided by y.
if (menu == 7) {
console.log(
`\nQuotient of "x"(${xValue}) / "y"(${yValue}) = ${calculate.divide()}\n`
.rainbow
);
}
// Returns the remainder of x divided by y.
if (Number(menu) == 8) {
console.log(
`\nRemainder of "x"(${xValue}) / "y"(${yValue}) = ${calculate.modulation()}\n`
.rainbow
);
}
// Returns x raised to the power of y.
if (Number(menu) == 9) {
console.log(
`\n"x"(${xValue}) raised to the power of "y"(${yValue}) = ${calculate.elevate()}\n`
.rainbow
);
}
// Returns the square root of x.
if (Number(menu) == 10) {
console.log(
`\nSquare root of "x"(${xValue}) = ${calculate.sqrt()}\n`.rainbow
);
}
// END - Class Instance
// FINAL ANIMATION
const rainbow = chalkAnimation.rainbow("See you later!!!"); // Animation starts
chalkAnimation.rainbow("I hope you had fun. See you later!!!", 1);
setTimeout(() => {
// Stop the animation, then write on a new line.
console.log("");
}, 1000);
// let str = 'Loading...';
// const loading = chalkAnimation.rainbow(str);
// // Add a new dot every second
// setInterval(() => {
// loading.replace(str += '.');
// }, 1000);