Skip to content

Commit ee7e6d4

Browse files
Alex JamshidiAlex Jamshidi
authored andcommitted
added solutions to sum.js in implement
1 parent 357426d commit ee7e6d4

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

Sprint-1/implement/sum.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function sum(list) {
2-
return 0
2+
const numbers = list.filter(n => Number.isFinite(n));
3+
let total = 0;
4+
for (n of numbers) {total += n};
5+
return total;
36
}
47

58
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ describe("sum", () => {
5050
// Then it should ignore the non-numerical values and return the sum of the numerical elements
5151

5252
it("array with non-number values returns sum of numerical values only", () => {
53-
const list = [1];
54-
expect(sum(list)).toEqual(1);
53+
const list = [1, 2, "apple", 4, "banana", 3];
54+
expect(sum(list)).toEqual(10);
5555
});
5656

5757
// Given an array with only non-number values
5858
// When passed to the sum function
5959
// Then it should return the least surprising value given how it behaves for all other inputs
6060

6161
it("array with non-number values only returns least surprising output", () => {
62-
const list = [1];
62+
const list = ["a", "b", "c"];
6363
expect(sum(list)).toEqual(0);
6464
});
6565
});

0 commit comments

Comments
 (0)