From dba1f99f109e4facc350e48c026046603014ac6b Mon Sep 17 00:00:00 2001 From: riyaalexander Date: Wed, 19 Oct 2022 16:52:25 -0400 Subject: [PATCH] javascript on your machine --- index.js | 31 ++++++++++++++++++++++++++++++- package-lock.json | 1 + 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 3bb4d07..53523cb 100644 --- a/index.js +++ b/index.js @@ -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]) + 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; 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": {