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

javascript on your machine #221

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
31 changes: 30 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,36 @@
* 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() {
console.log(process.argv)
let operator = process.argv[2]
// checking for no operation
if (!operator){
return "No operation provided..."
}
// checking for numbers
if (process.argv[3] === undefined){
return "No numbers provided..."
}
// check for other operators that are not plus or minus
if (!(operator.includes("plus") || operator.includes('minus'))) {
return `Invalid operation: ${operator}`
}
// for loop: initialize, condition, change
let sum = Number(process.argv[3])

Choose a reason for hiding this comment

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

Since you can add or subtract to the number a more accurate variable name might be something like total or result

for (let i = 4; i < process.argv.length; i++) {
// plus operation
if (operator === "plus") {
sum += Number(process.argv[i])
} else if (operator === "minus") {
sum -= process.argv[i]
}
}// end of for loop

return sum
}// end of function



// 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.