From 2249b5523c905edae391669d07326bd0724d30ee Mon Sep 17 00:00:00 2001 From: Mohammed Nabeel Date: Sat, 18 Jul 2020 14:54:58 +0300 Subject: [PATCH] sumAll done --- sumAll/sumAll.js | 28 ++++++++++++++++++++++++++-- sumAll/sumAll.spec.js | 38 +++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/sumAll/sumAll.js b/sumAll/sumAll.js index 4030fe8..9d5d216 100644 --- a/sumAll/sumAll.js +++ b/sumAll/sumAll.js @@ -1,5 +1,29 @@ -const sumAll = function() { +const sumAll = function(...args) { + + let totalOfNumbers = 0; + let firstNumber = args[0]; + let lastNumber = args[args.length - 1]; + /* If either is not a number, return ERROR */ + if (typeof firstNumber != "number" || typeof lastNumber != "number") { + return 'ERROR'; + } + /* If first number is greater, switch numbers */ + if (firstNumber > lastNumber) { + let tempNumber = firstNumber; + firstNumber = lastNumber; + lastNumber = tempNumber; + } + + /*If either number is negative, return 'ERROR' */ + if (firstNumber < 0 || lastNumber < 0) { + return 'ERROR'; + } + + for (let i = firstNumber; i <= lastNumber; i++) { + totalOfNumbers += i; + } + return totalOfNumbers; } -module.exports = sumAll +module.exports = sumAll \ No newline at end of file diff --git a/sumAll/sumAll.spec.js b/sumAll/sumAll.spec.js index 520adc1..a949839 100644 --- a/sumAll/sumAll.spec.js +++ b/sumAll/sumAll.spec.js @@ -1,22 +1,22 @@ const sumAll = require('./sumAll') describe('sumAll', function() { - it('sums numbers within the range', function() { - expect(sumAll(1, 4)).toEqual(10); - }); - xit('works with large numbers', function() { - expect(sumAll(1, 4000)).toEqual(8002000); - }); - xit('works with larger number first', function() { - expect(sumAll(123, 1)).toEqual(7626); - }); - xit('returns ERROR with negative numbers', function() { - expect(sumAll(-10, 4)).toEqual('ERROR'); - }); - xit('returns ERROR with non-number parameters', function() { - expect(sumAll(10, "90")).toEqual('ERROR'); - }); - xit('returns ERROR with non-number parameters', function() { - expect(sumAll(10, [90, 1])).toEqual('ERROR'); - }); -}); + it('sums numbers within the range', function() { + expect(sumAll(1, 4)).toEqual(10); + }); + it('works with large numbers', function() { + expect(sumAll(1, 4000)).toEqual(8002000); + }); + it('works with larger number first', function() { + expect(sumAll(123, 1)).toEqual(7626); + }); + it('returns ERROR with negative numbers', function() { + expect(sumAll(-10, 4)).toEqual('ERROR'); + }); + it('returns ERROR with non-number parameters', function() { + expect(sumAll(10, "90")).toEqual('ERROR'); + }); + it('returns ERROR with non-number parameters', function() { + expect(sumAll(10, [90, 1])).toEqual('ERROR'); + }); +}); \ No newline at end of file