8000 Finished sumAll function · Andy51011/javascript-exercises@bbe7ac1 · GitHub
[go: up one dir, main page]

Skip to content

Commit bbe7ac1

Browse files
committed
Finished sumAll function
1 parent afddb60 commit bbe7ac1

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

sumAll/sumAll.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1-
const sumAll = function() {
2-
1+
const sumAll = function(val1, val2) {
2+
if (typeof val1 !== "number" || typeof val2 !== "number") {
3+
return "ERROR"
4+
}
5+
if (val1 < 0 || val2 < 0) {
6+
return "ERROR"
7+
}
8+
var sum = 0
9+
if (val1 < val2) {
10+
for (var i = val1; i <= val2; i++) {
11+
sum += i
12+
}
13+
}
14+
if (val1 > val2) {
15+
for (var i = val2; i <= val1; i++) {
16+
sum += i
17+
}
18+
}
19+
return sum
320
}
421

522
module.exports = sumAll

sumAll/sumAll.spec.js

Lines changed: 5 additions & 5 deletions
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)
0