From 2b98aaf97d4bfb55eca82ccab197cb8ba0fef4c0 Mon Sep 17 00:00:00 2001 From: aardvarkpepper Date: Sun, 16 Oct 2022 19:43:06 -0400 Subject: [PATCH 1/2] Modify index.js with code that passes the tests --- index.js | 25 ++++++++++++++++++++++++- package-lock.json | 1 + 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 3bb4d07..6fa1dd9 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,30 @@ * 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 tString = process.argv[2]; + let sumV = 0; + if (!tString) { + return "No operation provided..."; + } else if (tString !== "plus" && tString !== "minus"){ + if (isNaN(parseInt(tString))){ + return `Invalid operation: ${process.argv[2]}`; + } + } else if (!process.argv[3]) { + return "No numbers provided..." + } else if (tString === "plus") { + for (let i = 3; i < process.argv.length; i++) { + sumV += parseInt(process.argv[i]); + } + return sumV; + } else if (tString === "minus") { + sumV = process.argv[3]; + for (let i = 4; i < process.argv.length; i++) { + sumV -= parseInt(process.argv[i]); + } + return sumV; + } +} // 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": { From ef27b25727f283a9981fe6a2f3b575c3346e2e06 Mon Sep 17 00:00:00 2001 From: aardvarkpepper Date: Sun, 16 Oct 2022 21:18:21 -0400 Subject: [PATCH 2/2] Add gratuitous 'hamster' error if process.argv[2] is a number. Discovered previous commit did not account for this case, though tests passed. Tests still pass. --- index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.js b/index.js index 6fa1dd9..45f4c54 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,9 @@ function calculator() { if (isNaN(parseInt(tString))){ return `Invalid operation: ${process.argv[2]}`; } + if (typeof parseInt(tString) === "number") { + return "No hamster provided..."; + } } else if (!process.argv[3]) { return "No numbers provided..." } else if (tString === "plus") { @@ -26,6 +29,8 @@ function calculator() { sumV -= parseInt(process.argv[i]); } return sumV; + } else { + return `No hamster provided...` } }