diff --git a/index.js b/index.js index 3bb4d07..d272b42 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,35 @@ * 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 array = process.argv + let result = 0; + + if (array.length < 3){ + return 'No operation provided...' + } + else if (!array[3]){ + return 'No numbers provided...' + } + if (array[2] === 'plus') { + for (let i = 3; i < array.length; i++) { + result += Number(array[i]); + } + return result + } + else if (array[2] === 'minus'){ + result = array[3] + for (let i = 4; i < array.length; i++){ + result -= Number(array[i]); + } + return result + } + + else { + return `Invalid operation: ${array[2]}`; + } + } +//for(let i = 0; i <= process.argv.length; i++) { // 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": {