Skip to content

I passed my test 🦜 #6

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

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
24 changes: 23 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,29 @@
* 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() {
const args = process.argv.slice(2);
const operation = args[0];
const numbers = args.slice(1).map(Number);

if (!operation) {
return "No operation provided...";
}

if (operation !== "plus" && operation !== "minus") {
return `Invalid operation: ${operation}`;
}

if (numbers.length === 0) {
return "No numbers provided...";
}

if (operation === "plus") {
return numbers.reduce((acc, curr) => acc + curr, 0);
} else {
return numbers.reduce((acc, curr) => acc - curr);
}
}

// Don't change anything below this line.
module.exports = calculator;