Skip to content

Commit 8b522de

Browse files
committed
updated the code base on the general feedbacks
1 parent 5c3214a commit 8b522de

4 files changed

Lines changed: 16 additions & 5 deletions

File tree

Sprint-1/implement/dedupe.test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,20 @@ test("given an empty array, it returns an empty array", () => {
2828
// Then it should return a copy of the original array
2929

3030
test("Given an array with no duplicates, it returns an the same array", () => {
31-
const currentOutput = dedupe([1,4,6,"d","a","x","e",0,7,8]);
31+
const input = [1,4,6,"d","a","x","e",0,7,8];
32+
const currentOutput = dedupe(input);
3233
const targetOutput = [1,4,6,"d","a","x","e",0,7,8];
3334

3435
expect(currentOutput).toEqual(targetOutput);
3536
});
37+
38+
test("The copy must not be the very same array in memory", () => {
39+
const input = [3, 1, 2];
40+
const currentOutput = dedupe(input);
41+
42+
expect(currentOutput).toEqual([3, 1, 2]);
43+
expect(currentOutput).not.toBe(input); // make sure the function returns a copy
44+
});
3645
// Given an array with strings or numbers
3746
// When passed to the dedupe function
3847
// Then it should remove the duplicate values, preserving the first occurrence of each element
@@ -42,4 +51,4 @@ test("Given an array with strings or numbers, it returns an the array whit no du
4251
const targetOutput = ['a','b','c'];
4352

4453
expect(currentOutput).toEqual(targetOutput);
45-
});
54+
});

Sprint-1/implement/max.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ function findMax(elements) {
88
for (let i = 0; i < elements.length; i++) {
99
let current = elements[i];
1010

11-
if (typeof current === "number") {
11+
// using Number.isFinite keeps NaN or Infinity out of the comparison
12+
if (typeof current === "number" && Number.isFinite(current)) {
1213
if (current > max) {
1314
max = current;
1415
}

Sprint-1/implement/sum.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ 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" && !Number.isNaN(current)) {
10+
// Number.isFinite skips NaN and Infinity, so only real numbers add to total
11+
if (typeof current === "number" && Number.isFinite(current)) {
1112
total = total + current; // add to total
1213
}
1314
}

Sprint-1/implement/sum.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ test("given an array whit a decimal/float number, returns correct total (decima
5151
const currentOutput = sum([1,2,3,4,3.5,]);
5252
const targetPitPut= 13.5;
5353

54-
expect(currentOutput).toEqual(targetPitPut);
54+
expect(currentOutput).toBeCloseTo(targetPitPut); // allow tiny floating point differences
5555
})
5656

5757

0 commit comments

Comments
 (0)