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

completed lab #217

Open
wants to merge 1 commit 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
26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,31 @@
* 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() {
// ✕ should return an error message if no operation is provided (1 ms) "No operation provided..."
// ✕ should return an error message if no numbers are provided "No numbers provided..."
// ✕ should return an error message if the operation does not match 'plus' or 'minus' "Invalid operation: modulo"
let total = 0
if(!process.argv[2]){
return "No operation provided..."
} else if (!process.argv [3]){
return "No numbers provided..."
} else if (process.argv[2] !== "plus" && process.argv[2] !== "minus"){
return `Invalid operation: ${process.argv[2]}`
}
if(process.argv[2] === "plus"){
for(let i = 3; i<process.argv.length; i++){
total += Number(process.argv[i])
}
return total
} else if(process.argv[2] === "minus"){
total = Number(process.argv[3])
for(let i=4; i<process.argv.length; i++){ //we start at 4 since we assigned total the value of the third index of process.argv
total -= Number(process.argv[i])
}
return total
}
}

// Don't change anything below this line.
module.exports = calculator;
1 change: 1 addition & 0 deletions package-lock.json

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