diff --git a/index.js b/index.js index 3bb4d07..ad3951f 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,33 @@ +const { run } = require("jest"); + /** * 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. * @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); + 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]}`; + }else if ( process.argv[2] === 'plus' ){ + let sum = 0 + for(let i = 3; i < process.argv.length; i++){ + sum += Number( process.argv[i]) + } + return sum; + } else if ( process.argv[2] === 'minus'){ + let product = Number( process.argv[3] ) + for(let i = 4; i < process.argv.length;i++){ + product -= Number( process.argv[i] ) + } + return product; +} +} // Don't change anything below this line. module.exports = calculator; diff --git a/package-lock.json b/package-lock.json index 99b1c39..ce6f0ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "8-0-javascript-on-your-machine-lab", "version": "1.0.0", "license": "ISC", "devDependencies": {