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 lab #227

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
87 changes: 86 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,92 @@
* 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[2])

let operation = process.argv[2]

let numbers = process.argv.slice(3)

if(operation === undefined){

return "No operation provided..."

} if(operation !== "plus" || operation !== "minus"){

return `Invalid operation: ${operation}`

} if(numbers.length = 0){

return "No numbers provided..."

} if(operation === 'plus'){

let sum = 0

for(let i = 0; i < numbers.length; i++){

sum += numbers[i]

}

return sum

} else if(operation === "minus"){

let sum = number[0]

for(let i = 1; i < numbers.length; i++){

sum -= numbers[i]
}


return sum
}

// Don't change anything below this line.
module.exports = calculator;
//
//
//
// if(operation === undefined){
// return "No operation provided..."
//} else if(operation !== "plus" || operation !== "minus"){

// return `Invalid operation: modulo`
//}
//
// if(operation === "plus"){
//
//
//for(let i = 3; i < process.argv.length; i++){
//
// number += +process.argv[i]
// return number
//}
//
//
//
//} else if(operation === "minus"){
//
// for(let i = 3; i < process.argv.length; i++){
//
// number -= +process.argv[i]
// return number
//
//} else {
// return "No numbers provided..."
//}
//
//}
//
//
//
// process.argv[0] and process.argv[1] will not change
// process.argv[2] will be "plus" or "minus"
// process.argv[i] will be the number
// process.argv[2] = operation
//for(let i = 3; i < process.argv.length; i++)
// let number
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.