diff --git a/index.js b/index.js index 3bb4d07..d206347 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,66 @@ * 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() {} +// CLEAN CODE: +function calculator() { + // assigning the operation if there's any + const command = process.argv[2]; + // assigning the first number if there's number + const number = process.argv[3]; + //converting the String type of number to the Number type + let result = Number(number); + + // if there's no operation + if (command === undefined) { + return "No operation provided..."; + // if there's no number + } else if (number === undefined) { + return "No numbers provided..."; + } + // looping from second number, + // because the first number is assigned to the variable result + for (let i = 4; i < process.argv.length; i++) { + if (command === 'plus') { + // adding to the first number + result += Number(process.argv[i]); + } else if (command === 'minus') { + // subtracting the first number + result -= Number(process.argv[i]); + } else { + // if the operation is not plus or minus + return `Invalid operation: ${command}`; + } + } + // return the total value; + return result; +} + + +// // MESSY CODE: +// let result = 0; +// function calculator() { +// if (process.argv[2] === undefined) { +// return "No operation provided..."; +// } else if (process.argv[3] === undefined) { +// return "No numbers provided..."; +// } else { +// let arr = []; +// for (let i = 3; i < process.argv.length; i++) { +// arr.push(parseInt(process.argv[i])); +// } +// for (let i = 0; i < arr.length; i++) { +// if (process.argv[2] === 'plus') { +// result = arr.reduce((a, b) => a + b); +// } else if (process.argv[2] === 'minus') { +// result = arr.reduce((a, b) => a - b); +// } else { +// return `Invalid operation: ${process.argv[2]}`; +// } +// } +// } +// return result; +// } + // 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": {