-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
finally finished lab #225
base: main
Are you sure you want to change the base?
finally finished lab #225
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,44 @@ | |
* 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() {} | ||
|
||
//function calculator() { | ||
// let argvs = process.argv.slice(2) | ||
// if (argvs < 1 ){ | ||
// return "No operation provided..." | ||
// } else if (argvs[0] === "plus" || argvs[0] === "minus" && ) | ||
|
||
// if (argvs[0] === "plus"){ | ||
// return a + b | ||
// } else if (argvs[0] === "minus"){ | ||
// return a - b | ||
// } | ||
// return a + b; | ||
// } | ||
function calculator() { | ||
if (!process.argv[2]) { | ||
return "No operation provided..."; | ||
} if (!process.argv[3]){ | ||
return "No numbers provided..."; | ||
} if (process.argv[2] === "plus") { | ||
let newArg = process.argv.slice(3); | ||
let sum = 0; | ||
sum = newArg.reduce((accumulator, value) => { | ||
return accumulator + +value}, 0); | ||
return sum; | ||
} if( process.argv[2] === 'minus'){ | ||
let newArg = process.argv.slice(3); | ||
let result = newArg[0] | ||
for(let i = 1; i < newArg.length; i++){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with reduce and this loop, you have two accumulators, how can you refactor your code to not be repetitive. |
||
result -= Number(newArg[i]) | ||
} | ||
return result | ||
} if (process.argv[2] !== "plus" || process.argv[2] !== "minus") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If i run |
||
return "Invalid operation: modulo" | ||
} | ||
} | ||
// const commandPlus = "plus"; | ||
// const commandMinus = "minus"; | ||
// const numbers = ["5", "10"]; | ||
// process.argv.push(command, ...numbers); | ||
// Don't change anything below this line. | ||
module.exports = calculator; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can declare sum here instead of line 27 so :
let sum = newArg.reduce(...)