8000 sumAll exercise · fivan18/javascript-exercises@097b2ed · GitHub
[go: up one dir, main page]

Skip to content

Commit 097b2ed

Browse files
author
Ivan
committed
sumAll exercise
1 parent cb2dfaa commit 097b2ed

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

sumAll/sumAll.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
const sumAll = function() {
1+
const sumAll = function(...args) {
2+
if(
3+
isNaN(args[0]) || isNaN(args[1]) ||
4+
args[0] < 0 || args[1] < 0 ||
5+
typeof args[0] === "string" || typeof args[1] === "string"
6+
){
7+
return 'ERROR';
8+
}
29

10+
args.sort((a,b) => a-b);
11+
12+
let count = 0;
13+
for(let i = args[0]; i <= args[1]; i++){
14+
count += i;
15+
}
16+
return count;
17+
318
}
419

520
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