Skip to content

Commit 98b044c

Browse files
committed
Does not modify the input array
1 parent c718f20 commit 98b044c

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

Sprint-1/fix/median.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function calculateMedian(list) {
1313
// filter out non-numbers and then sort
1414
const filteredList = list
1515
.filter((item) => typeof item === "number")
16-
.sort((a, b) => a - b);
16+
.toSorted((a, b) => a - b);
1717

1818
if (filteredList.length === 0) {
1919
return null;

Sprint-1/fix/median.test.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe("calculateMedian", () => {
1313
{ input: [1, 2, 3, 4], expected: 2.5 },
1414
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5 },
1515
].forEach(({ input, expected }) =>
16-
it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
16+
it(`returns the median for [${input}]`, () =>
17+
expect(calculateMedian(input)).toEqual(expected))
1718
);
1819

1920
[
@@ -24,7 +25,8 @@ describe("calculateMedian", () => {
2425
{ input: [110, 20, 0], expected: 20 },
2526
{ input: [6, -2, 2, 12, 14], expected: 6 },
2627
].forEach(({ input, expected }) =>
27-
it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
28+
it(`returns the correct median for unsorted array [${input}]`, () =>
29+
expect(calculateMedian(input)).toEqual(expected))
2830
);
2931

3032
it("doesn't modify the input array [3, 1, 2]", () => {
@@ -33,8 +35,17 @@ describe("calculateMedian", () => {
3335
expect(list).toEqual([3, 1, 2]);
3436
});
3537

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))
38+
[
39+
"not an array",
40+
123,
41+
null,
42+
undefined,
43+
{},
44+
[],
45+
["apple", null, undefined],
46+
].forEach((val) =>
47+
it(`returns null for non-numeric array (${val})`, () =>
48+
expect(calculateMedian(val)).toBe(null))
3849
);
3950

4051
[
@@ -45,6 +56,7 @@ describe("calculateMedian", () => {
4556
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
4657
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
4758
].forEach(({ input, expected }) =>
48-
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
59+
it(`filters out non-numeric values and calculates the median for [${input}]`, () =>
60+
expect(calculateMedian(input)).toEqual(expected))
4961
);
5062
});

0 commit comments

Comments
 (0)