Skip to content

Lab 4 i think: minus minus section #17

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 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
33 changes: 31 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
/**
* 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.
* 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 operator = process.argv[2]; //let operator = "operationName"
let sum = 0;


if (!operator) {//is there an operator?
return "No operation provided..."; //truthyness determined
} else if (operator !== "plus" && operator !== "minus") { //no math allowed
return "Invalid operation: " + operator;
}

const numbers = process.argv.slice(3); //look at every element after [2]
//let numArray = [];
if (numbers.length === 0) { //are there less than 2 elements
//console.log("first", numbers.length)
return "No numbers provided...";
}

for( let num of numbers) { //iterate through [3] onwards
if (operator === "plus") {
sum += Number(num); //accumulate everything including && after [3]
}
else if (operator === "minus") {
return sum = numbers.reduce((previousVal, currentVal) => Number(previousVal) - Number(currentVal)) //accummulate the difference of numbers
}
}
return sum;
}

calculator();

// Don't change anything below this line.
module.exports = calculator;
44 changes: 13 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.