Skip to content

Commit 0d9e98c

Browse files
committed
Sum Excercise
1 parent 4938239 commit 0d9e98c

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

Sprint-1/implement/sum.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
function sum(elements) {
1+
function sum(theArray) {
2+
3+
let onyNumbers = theArray.filter(item => typeof item === 'number');
4+
5+
6+
const theFinalSum = onyNumbers.reduce((runningTotal, currentNumber) => {
7+
return runningTotal + currentNumber;
8+
}, 0);
9+
10+
return theFinalSum;
211
}
312

13+
console.log (sum(["hey", 10, "hi", 60, 10]));
14+
415
module.exports = sum;
16+
17+
//numbersList.filter((item) => typeof item === "number");
18+
/* Sum the numbers in an array
19+
20+
In this kata, you will need to implement a function that sums the numerical elements of an array
21+
22+
E.g. sum([10, 20, 30]), target output: 60
23+
E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements)
24+
*/

Sprint-1/implement/sum.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,55 @@ const sum = require("./sum.js");
1313
// Given an empty array
1414
// When passed to the sum function
1515
// Then it should return 0
16-
test.todo("given an empty array, returns 0")
16+
test("given an empty array, returns 0", () => {
17+
const inputNumber = [];
18+
const result = sum(inputNumber);
19+
expect(result).toEqual(0);
20+
});
1721

1822
// Given an array with just one number
1923
// When passed to the sum function
2024
// Then it should return that number
2125

26+
test("given an array with 1 number, it returns the sam number", () => {
27+
const inputNumber = [34];
28+
const result = sum(inputNumber);
29+
expect(result).toEqual(34);
30+
});
31+
2232
// Given an array containing negative numbers
2333
// When passed to the sum function
2434
// Then it should still return the correct total sum
2535

36+
test("given an array containing negative numbers",() => {
37+
const inputNegative = [10, -5, 20, -15];
38+
const result= sum(inputNegative )
39+
expect (result).toEqual(10);
40+
});
41+
2642
// Given an array with decimal/float numbers
2743
// When passed to the sum function
2844
// Then it should return the correct total sum
45+
test("Given an array with decimal/float numbers", () => {
46+
const inputFloat = [10.2, 10.3];
47+
const result = sum(inputFloat);
48+
expect (result).toEqual(20.5);
49+
});
2950

3051
// Given an array containing non-number values
3152
// When passed to the sum function
3253
// Then it should ignore the non-numerical values and return the sum of the numerical elements
54+
test("Given an array containing non-number values", () => {
55+
const inputNonNumbers = [1, "apple", 2, true, 3];
56+
const result = sum(inputNonNumbers);
57+
expect (result).toEqual(6)
58+
});
3359

3460
// Given an array with only non-number values
3561
// When passed to the sum function
3662
// Then it should return the least surprising value given how it behaves for all other inputs
63+
test("Given an array with only non-number values" , () =>{
64+
const nonNumbers = ["helo", "hi"];
65+
const result = sum(nonNumbers);
66+
expect (result).toEqual(0)
67+
});

0 commit comments

Comments
 (0)