Skip to content

Commit f6f580c

Browse files
committed
wrote the test for sum.js and updated the sum function to check if the input is not NaN
1 parent 9f0d9c4 commit f6f580c

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

Sprint-1/implement/sum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function sum(elements) {
77
let current = elements[i]; // get the current element
88

99
// Only add it if it's a number
10-
if (typeof current === "number") {
10+
if (typeof current === "number" && !Number.isNaN(current)) {
1111
total = total + current; // add to total
1212
}
1313
}

Sprint-1/implement/sum.test.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,64 @@ 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 currentOutput = sum([]);
18+
const targetPitPut= 0;
19+
20+
expect(currentOutput).toEqual(targetPitPut);
21+
})
22+
1723

1824
// Given an array with just one number
1925
// When passed to the sum function
2026
// Then it should return that number
27+
test("given an array whit only 1 number, returns same input",()=>{
28+
const currentOutput = sum([4]);
29+
const targetPitPut= 4;
30+
31+
expect(currentOutput).toEqual(targetPitPut);
32+
})
33+
2134

2235
// Given an array containing negative numbers
2336
// When passed to the sum function
2437
// Then it should still return the correct total sum
38+
test("given an array whit a negative number, returns correct total (subtract the negative number)",()=>{
39+
const currentOutput = sum([1,2,3,4,-5,]);
40+
const targetPitPut= 5;
41+
42+
expect(currentOutput).toEqual(targetPitPut);
43+
})
44+
2545

2646
// Given an array with decimal/float numbers
2747
// When passed to the sum function
2848
// Then it should return the correct total sum
2949

50+
test("given an array whit a decimal/float number, returns correct total (decimal/float number)",()=>{
51+
const currentOutput = sum([1,2,3,4,3.5,]);
52+
const targetPitPut= 13.5;
53+
54+
expect(currentOutput).toEqual(targetPitPut);
55+
})
56+
57+
3058
// Given an array containing non-number values
3159
// When passed to the sum function
3260
// Then it should ignore the non-numerical values and return the sum of the numerical elements
61+
test("given an array whit a non-number values, returns correct total (ignore the NaN and sum the others)",()=>{
62+
const currentOutput = sum([1,2,3,4,"hi",5]);
63+
const targetPitPut= 15;
64+
65+
expect(currentOutput).toEqual(targetPitPut);
66+
})
3367

3468
// Given an array with only non-number values
3569
// When passed to the sum function
3670
// Then it should return the least surprising value given how it behaves for all other inputs
71+
test("given an array with only non-number values, returns the least surprising value (0)", () => {
72+
const currentOutput = sum(["hello", null, undefined, {}, [], true, NaN]);
73+
const targetOutput = 0;
74+
75+
expect(currentOutput).toEqual(targetOutput);
76+
});

0 commit comments

Comments
 (0)