Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {

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(...)

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++){

Choose a reason for hiding this comment

The 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") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modulo is hardcoded here. What if I use the operator division?

If i run node run.js 1 2 3, I get Invalid operation: modulo. How can you update this conditional to make sure you get the correct error message?

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;