Skip to content

Commit 93f82db

Browse files
committed
Fix: fixed fix and includes
1 parent 65bf6f1 commit 93f82db

4 files changed

Lines changed: 33 additions & 43 deletions

File tree

Sprint-1/fix/median.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
function calculateMedian(list) {
2-
32
// must be an array
43
if (!Array.isArray(list)) {
54
return null;
65
}
76

8-
// keep only numeric values
9-
const numbers = list.filter(value => typeof value === "number");
7+
// keep only VALID numeric values (exclude NaN, Infinity)
8+
const numbers = list.filter(
9+
(value) => typeof value === "number" && Number.isFinite(value)
10+
);
1011

1112
// if no numbers exist return null
1213
if (numbers.length === 0) {

Sprint-1/fix/median.test.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
// median.test.js
2-
3-
// Someone has implemented calculateMedian but it isn't
4-
// passing all the tests...
5-
// Fix the implementation of calculateMedian so it passes all tests
6-
71
const calculateMedian = require("./median.js");
82

93
describe("calculateMedian", () => {
@@ -13,7 +7,8 @@ describe("calculateMedian", () => {
137
{ input: [1, 2, 3, 4], expected: 2.5 },
148
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5 },
159
].forEach(({ input, expected }) =>
16-
it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
10+
it(`returns the median for [${input}]`, () =>
11+
expect(calculateMedian(input)).toEqual(expected))
1712
);
1813

1914
[
@@ -24,7 +19,8 @@ describe("calculateMedian", () => {
2419
{ input: [110, 20, 0], expected: 20 },
2520
{ input: [6, -2, 2, 12, 14], expected: 6 },
2621
].forEach(({ input, expected }) =>
27-
it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
22+
it(`returns the correct median for unsorted array [${input}]`, () =>
23+
expect(calculateMedian(input)).toEqual(expected))
2824
);
2925

3026
it("doesn't modify the input array [3, 1, 2]", () => {
@@ -33,8 +29,9 @@ describe("calculateMedian", () => {
3329
expect(list).toEqual([3, 1, 2]);
3430
});
3531

36-
[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
37-
it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null))
32+
['not an array', 123, null, undefined, {}, [], ["apple", null, undefined]].forEach(val =>
33+
it(`returns null for non-numeric array (${val})`, () =>
34+
expect(calculateMedian(val)).toBe(null))
3835
);
3936

4037
[
@@ -45,6 +42,12 @@ describe("calculateMedian", () => {
4542
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
4643
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
4744
].forEach(({ input, expected }) =>
48-
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
45+
it(`filters out non-numeric values and calculates the median for [${input}]`, () =>
46+
expect(calculateMedian(input)).toEqual(expected))
4947
);
50-
});
48+
49+
// 🔥 EXTRA test for reviewer feedback
50+
it("ignores NaN and Infinity values", () => {
51+
expect(calculateMedian([1, 2, NaN, Infinity, 3])).toEqual(2);
52+
});
53+
});

Sprint-1/refactor/includes.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
// Refactor the implementation of includes to use a for...of loop
2-
31
function includes(list, target) {
4-
for (let index = 0; index < list.length; index++) {
5-
const element = list[index];
2+
// handle non-array safely (optional but solid)
3+
if (!Array.isArray(list)) {
4+
return false;
5+
}
6+
7+
for (const element of list) {
68
if (element === target) {
79
return true;
810
}
911
}
12+
1013
return false;
1114
}
1215

13-
module.exports = includes;
16+
module.exports = includes;

Sprint-1/refactor/includes.test.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,21 @@
1-
// Refactored version of includes should still pass the tests below:
2-
31
const includes = require("./includes.js");
42

53
test("returns true when target is in array", () => {
6-
const currentOutput = includes(["a", "b", "c", "d"], "c");
7-
const targetOutput = true;
8-
9-
expect(currentOutput).toEqual(targetOutput);
4+
expect(includes(["a", "b", "c", "d"], "c")).toEqual(true);
105
});
116

127
test("returns false when target not in array", () => {
13-
const currentOutput = includes([1, 2, 3, 4], "a");
14-
const targetOutput = false;
15-
16-
expect(currentOutput).toEqual(targetOutput);
8+
expect(includes([1, 2, 3, 4], "a")).toEqual(false);
179
});
1810

1911
test("returns true when the target is in array multiple times", () => {
20-
const currentOutput = includes([1, 2, 2, 3], 2);
21-
const targetOutput = true;
22-
23-
expect(currentOutput).toEqual(targetOutput);
12+
expect(includes([1, 2, 2, 3], 2)).toEqual(true);
2413
});
2514

2615
test("returns false for empty array", () => {
27-
const currentOutput = includes([]);
28-
const targetOutput = false;
29-
30-
expect(currentOutput).toEqual(targetOutput);
16+
expect(includes([], "a")).toEqual(false); // ✅ FIXED
3117
});
3218

3319
test("searches for null", () => {
34-
const currentOutput = includes(["b", "z", null, "a"], null);
35-
const targetOutput = true;
36-
37-
expect(currentOutput).toEqual(targetOutput);
38-
});
20+
expect(includes(["b", "z", null, "a"], null)).toEqual(true);
21+
});

0 commit comments

Comments
 (0)