-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
99 lines (84 loc) · 2.7 KB
/
index.mjs
File metadata and controls
99 lines (84 loc) · 2.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
// import { info, warning, error, debug, Logger } from '@gamunetwork/logger';
import { info, critical, error, debug, Logger, LEVELS } from '../../dist/index.js';
function addNumbers(a, b) {
debug('Adding numbers '+ a + ' and ' + b);
let result = a + b;
debug('Result '+ result);
return result;
}
function subtractNumbers(a, b) {
debug('Subtracting numbers '+ a + ' and ' + b);
let result = a - b;
debug('Result '+ result);
return result;
}
function multiplyNumbers(a, b) {
debug('Multiplying numbers '+ a + ' and ' + b);
let result = a * b;
debug('Result '+ result);
return result;
}
function divideNumbers(a, b) {
debug('Dividing numbers '+ a + ' and ' + b);
if (b === 0) {
error('Invalid operation: Cannot divide by zero');
throw new Error('Invalid operation: Cannot divide by zero')
}
let result = a / b;
debug('Result '+ result);
return result;
}
function parseArguments() {
// expect a, operator, b and optional debug flag
if (process.argv.length < 5) { // 3 arguments + 1 for node + 1 for script name
error('Invalid number of arguments. Expected 3 or 4, got ' + (process.argv.length - 2));
throw new Error('Invalid number of arguments');
}
let debugMode = false;
// remove debug flag from arguments
if (process.argv.includes('--debug')) {
debugMode = true;
process.argv = process.argv.filter(arg => arg !== '--debug');
}
if (process.argv.includes('-d')) {
debugMode = true;
process.argv = process.argv.filter(arg => arg !== '-d');
}
let a = parseFloat(process.argv[2]);
let operator = process.argv[3];
let b = parseFloat(process.argv[4]);
return { a, operator, b, debugMode };
}
function main() {
let a, operator, b, debugMode, result;
try{
({ a, operator, b, debugMode } = parseArguments());
if (debugMode) {
Logger.setLevel("stdout", LEVELS.DEBUG);
debug('Debug mode enabled');
}
switch(operator) {
case '+':
result = addNumbers(a, b);
break;
case '-':
result = subtractNumbers(a, b);
break;
case '*':
result = multiplyNumbers(a, b);
break;
case '/':
result = divideNumbers(a, b);
break;
default:
error('Invalid operator "' + operator + '"');
throw new Error('Invalid operator');
}
}
catch(e) {
critical(e.message);
process.exit(1);
}
info(a.toString() + operator + b.toString() + ' = ' + result.toString());
}
main();