8000 sumAll · M-Munk/javascript-exercises@35ed3d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 35ed3d7

Browse files
committed
sumAll
1 parent 02d2ca4 commit 35ed3d7

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

sumAll/sumAll.js

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

3-
}
4-
5-
module.exports = sumAll
16+
module.exports = sumAll;

sumAll/sumAll.spec.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
var sumAll = require('./sumAll')
1+
const sumAll = require("./sumAll");
22

3-
describe('sumAll', function() {
4-
it('sums numbers within the range', function() {
3+
describe("sumAll", () => {
4+
it("sums numbers within the range", () => {
55
expect(sumAll(1, 4)).toEqual(10);
66
});
7-
xit('works with large numbers', function() {
7+
it("works with large numbers", () => {
88
expect(sumAll(1, 4000)).toEqual(8002000);
99
});
10-
xit('works with larger number first', function() {
10+
it("works with larger number first", () => {
1111
expect(sumAll(123, 1)).toEqual(7626);
1212
});
13-
xit('returns ERROR with negative numbers', function() {
14-
expect(sumAll(-10, 4)).toEqual('ERROR');
13+
it("returns ERROR with negative numbers", () => {
14+
expect(sumAll(-10, 4)).toEqual("ERROR");
1515
});
16-
xit('returns ERROR with non-number parameters', function() {
17-
expect(sumAll(10, "90")).toEqual('ERROR');
16+
it("returns ERROR with non-number parameters", () => {
17+
expect(sumAll(10, "90")).toEqual("ERROR");
1818
});
19-
xit('returns ERROR with non-number parameters', function() {
20-
expect(sumAll(10, [90, 1])).toEqual('ERROR');
19+
it("returns ERROR with non-number parameters", () => {
20+
expect(sumAll(10, [90, 1])).toEqual("ERROR");
2121
});
2222
});

0 commit comments

Comments
 (0)
0