sumAll done

This commit is contained in:
Mohammed Nabeel 2020-07-18 14:54:58 +03:00
parent e8cdd37d95
commit 2249b5523c
2 changed files with 45 additions and 21 deletions

View File

@ -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

View File

@ -1,22 +1,22 @@
const sumAll = require('./sumAll') const sumAll = require('./sumAll')
describe('sumAll', function() { describe('sumAll', function() {
it('sums numbers within the range', function() { it('sums numbers within the range', function() {
expect(sumAll(1, 4)).toEqual(10); expect(sumAll(1, 4)).toEqual(10);
}); });
xit('works with large numbers', function() { it('works with large numbers', function() {
expect(sumAll(1, 4000)).toEqual(8002000); expect(sumAll(1, 4000)).toEqual(8002000);
}); });
xit('works with larger number first', function() { it('works with larger number first', function() {
expect(sumAll(123, 1)).toEqual(7626); expect(sumAll(123, 1)).toEqual(7626);
}); });
xit('returns ERROR with negative numbers', function() { it('returns ERROR with negative numbers', function() {
expect(sumAll(-10, 4)).toEqual('ERROR'); expect(sumAll(-10, 4)).toEqual('ERROR');
}); });
xit('returns ERROR with non-number parameters', function() { it('returns ERROR with non-number parameters', function() {
expect(sumAll(10, "90")).toEqual('ERROR'); expect(sumAll(10, "90")).toEqual('ERROR');
}); });
xit('returns ERROR with non-number parameters', function() { it('returns ERROR with non-number parameters', function() {
expect(sumAll(10, [90, 1])).toEqual('ERROR'); expect(sumAll(10, [90, 1])).toEqual('ERROR');
}); });
}); });