diff --git a/05_sumAll/sumAll.js b/05_sumAll/sumAll.js index 00880c7..875873d 100644 --- a/05_sumAll/sumAll.js +++ b/05_sumAll/sumAll.js @@ -1,6 +1,18 @@ -const sumAll = function() { +const sumAll = function (number1, number2) { + if (number1 < 0 || number2 < 0) return 'ERROR' + if (typeof number1 !== 'number' || typeof number2 !== 'number') return 'ERROR' -}; + let sum = 0 + + const start = Math.min(number1, number2) + const end = Math.max(number1, number2) + + for (let i = start; i <= end; i++) { + sum += i + } + + return sum +} // Do not edit below this line -module.exports = sumAll; +module.exports = sumAll diff --git a/05_sumAll/sumAll.spec.js b/05_sumAll/sumAll.spec.js index 1a9fb7c..a865931 100644 --- a/05_sumAll/sumAll.spec.js +++ b/05_sumAll/sumAll.spec.js @@ -2,21 +2,21 @@ const sumAll = require('./sumAll') describe('sumAll', () => { test('sums numbers within the range', () => { - expect(sumAll(1, 4)).toEqual(10); - }); - test.skip('works with large numbers', () => { - expect(sumAll(1, 4000)).toEqual(8002000); - }); - test.skip('works with larger number first', () => { - expect(sumAll(123, 1)).toEqual(7626); - }); - test.skip('returns ERROR with negative numbers', () => { - expect(sumAll(-10, 4)).toEqual('ERROR'); - }); - test.skip('returns ERROR with non-number parameters', () => { - expect(sumAll(10, "90")).toEqual('ERROR'); - }); - test.skip('returns ERROR with non-number parameters', () => { - expect(sumAll(10, [90, 1])).toEqual('ERROR'); - }); -}); + expect(sumAll(1, 4)).toEqual(10) + }) + test('works with large numbers', () => { + expect(sumAll(1, 4000)).toEqual(8002000) + }) + test('works with larger number first', () => { + expect(sumAll(123, 1)).toEqual(7626) + }) + test('returns ERROR with negative numbers', () => { + expect(sumAll(-10, 4)).toEqual('ERROR') + }) + test('returns ERROR with non-number parameters', () => { + expect(sumAll(10, '90')).toEqual('ERROR') + }) + test('returns ERROR with non-number parameters', () => { + expect(sumAll(10, [90, 1])).toEqual('ERROR') + }) +})