8000 Complete #5 · hellogabor/javascript-exercises@0d7b20e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d7b20e

Browse files
committed
1 parent d87a982 commit 0d7b20e

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

05_sumAll/sumAll.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1-
const sumAll = function() {
1+
const sumAll = function(a, b) {
2+
if (a > b) {
3+
let c = a;
4+
a = b;
5+
b = c;
6+
}
27

8+
if (a < 0 || b < 0) {
9+
return 'ERROR';
10+
}
11+
12+
if ((typeof a !== 'number') || (typeof b !== 'number')) {
13+
return 'ERROR';
14+
}
15+
16+
let allNumbers = [];
17+
let i = a;
18+
while (i <= b) {
19+
allNumbers.push(i);
20+
i++;
21+
}
22+
23+
let sum = 0;
24+
allNumbers.forEach((number) => {
25+
sum += number;
26+
});
27+
return sum;
328
};
429

30+
/* const sum = allNumbers.reduce((accumulator, currentValue) =>
31+
accumulator + currentValue, 0);
32+
return sum; */
33+
34+
535
// Do not edit below this line
636
module.exports = sumAll;

05_sumAll/sumAll.spec.js

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

0 commit comments

Comments
 (0)
0