Skip to content

Commit c97ce83

Browse files
committed
Complete sumAll challenge
1 parent cd06c46 commit c97ce83

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

sumAll/sumAll.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1-
const sumAll = function() {
2-
1+
const sumAll = function(a, b) {
2+
// Test for invalid arguments
3+
if (a <= 0 || b <= 0) {
4+
return "ERROR";
5+
}
6+
if (parseInt(a) !== a || parseInt(b) !== b) {
7+
return "ERROR";
8+
}
9+
// Define variables
10+
let all = [];
11+
let i, n;
12+
// Figure out which argument is smaller
13+
if (a < b) {
14+
i = a;
15+
n = b;
16+
} else {
17+
i = b;
18+
n = a;
19+
}
20+
// Create array of all numbers in range
21+
for (; i <= n; i++) {
22+
all.push(i);
23+
}
24+
// Sum up numbers in range and return sum
25+
let sum = all.reduce((x, y) => x += y);
26+
return sum;
327
}
428

529
module.exports = sumAll

sumAll/sumAll.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ describe('sumAll', function() {
44
it('sums numbers within the range', function() {
55
expect(sumAll(1, 4)).toEqual(10);
66
});
7-
xit('works with large numbers', function() {
7+
it('works with large numbers', function() {
88
expect(sumAll(1, 4000)).toEqual(8002000);
99
});
10-
xit('works with larger number first', function() {
10+
it('works with larger number first', function() {
1111
expect(sumAll(123, 1)).toEqual(7626);
1212
});
13-
xit('returns ERROR with negative numbers', function() {
13+
it('returns ERROR with negative numbers', function() {
1414
expect(sumAll(-10, 4)).toEqual('ERROR');
1515
});
16-
xit('returns ERROR with non-number parameters', function() {
16+
it('returns ERROR with non-number parameters', function() {
1717
expect(sumAll(10, "90")).toEqual('ERROR');
1818
});
19-
xit('returns ERROR with non-number parameters', function() {
19+
it('returns ERROR with non-number parameters', function() {
2020
expect(sumAll(10, [90, 1])).toEqual('ERROR');
2121
});
2222
});

0 commit comments

Comments
 (0)