forked from joinpursuit/8-0-javascript-on-your-machine-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (34 loc) · 1.37 KB
/
index.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
/**
* The function below has no parameters. Instead, access the arguments from the command line.
* The first argument passed after the filename should be either "plus" or "minus", which represents the kind of calculation that will be done. Every argument afterwards should be a number.
* Depending on the operation, either add up all of the numbers or subtract all of the numbers, from left to right.
* @returns {number} The result of either adding all numbers or subtracting all numbers, depending on the arguments added to the command line.
*/
function calculator() {
if(process.argv[2]=== `plus`){
let sum = 0
if(!parseInt(process.argv[3]) || !process.argv[3]){
return "No numbers provided..."
} else{
for (let i = 3; i < process.argv.length; i++) {
sum += parseInt(process.argv[i])
}
}
return sum
}
if(process.argv[2]===`minus`){
let sum = process.argv[3]
for(let i = 4; i < process.argv.length; i++){
sum -= parseInt(process.argv[i])
}
return sum
}
if(!process.argv[2]){
return "No operation provided..."
}
if(process.argv[2]!=='plus' || process.argv[2]!== 'minus'){
return "Invalid operation: modulo"
}
}
// Don't change anything below this line.
module.exports = calculator;