Skip to content

Commit b881892

Browse files
committed
Implemented the dedupe function and all tests pass.
1 parent f1f59ac commit b881892

6 files changed

Lines changed: 100 additions & 30 deletions

File tree

‎Sprint-1/fix/median.js‎

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,28 @@
55
// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
66
// or 'list' has mixed values (the function is expected to sort only numbers).
77

8-
98
function calculateMedian(list) {
10-
if (!Array.isArray(list)) {
11-
return null
12-
}
13-
const arrCopy = [...list]
14-
const filteredNumbers = arrCopy.filter((num => Number.isFinite(num)));
9+
if (!Array.isArray(list)) {
10+
return null;
11+
}
12+
const arrCopy = [...list];
13+
const filteredNumbers = arrCopy.filter((num) => Number.isFinite(num));
1514

16-
if (filteredNumbers.length === 0) {
17-
return null
18-
}
15+
if (filteredNumbers.length === 0) {
16+
return null;
17+
}
1918

20-
const sortedNumbers = filteredNumbers.sort((a, b) => a - b);
21-
let middleIndex
22-
if (sortedNumbers.length % 2 === 0) {
23-
middleIndex = sortedNumbers.length / 2
24-
const firstMiddleIndex = sortedNumbers[middleIndex - 1];
25-
const secondMiddleIndex = sortedNumbers[middleIndex];
26-
return (firstMiddleIndex + secondMiddleIndex) / 2
27-
} else {
28-
middleIndex = Math.floor(sortedNumbers.length / 2);
29-
return sortedNumbers[middleIndex];
30-
}
19+
const sortedNumbers = filteredNumbers.sort((a, b) => a - b);
20+
let middleIndex;
21+
if (sortedNumbers.length % 2 === 0) {
22+
middleIndex = sortedNumbers.length / 2;
23+
const firstMiddleIndex = sortedNumbers[middleIndex - 1];
24+
const secondMiddleIndex = sortedNumbers[middleIndex];
25+
return (firstMiddleIndex + secondMiddleIndex) / 2;
26+
} else {
27+
middleIndex = Math.floor(sortedNumbers.length / 2);
28+
return sortedNumbers[middleIndex];
29+
}
3130
}
3231

3332
// console.log(calculateMedian([100, 2, 3, 'f', 4, 9, 9, 0, "hey", 'g']))

‎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
});

‎Sprint-1/implement/dedupe.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
function dedupe() {}
1+
function dedupe(elements) {
2+
if (elements.length === 0) {
3+
return [];
4+
}
5+
6+
return elements.filter((item, index) => elements.indexOf(item) === index);
7+
}
8+
9+
module.exports = dedupe;

‎Sprint-1/implement/dedupe.test.js‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,46 @@ E.g. dedupe([1, 2, 1]) returns [1, 2]
1616
// Given an empty array
1717
// When passed to the dedupe function
1818
// Then it should return an empty array
19-
test.todo("given an empty array, it returns an empty array");
19+
test("given an empty array, return an empty array", () => {
20+
expect(dedupe([])).toEqual([]);
21+
});
2022

2123
// Given an array with no duplicates
2224
// When passed to the dedupe function
2325
// Then it should return a copy of the original array
26+
const testCaseNoDuplicates = [
27+
{
28+
input: ["hey", "buddy", "world", "10", "sister", 1],
29+
expected: ["hey", "buddy", "world", "10", "sister", 1],
30+
},
31+
{
32+
input: [2, 3, 4, 5, 6, 7],
33+
expected: [2, 3, 4, 5, 6, 7],
34+
},
35+
];
36+
testCaseNoDuplicates.forEach(({ input, expected }) => {
37+
test("given an array with no duplicates, return a copy of the array", () => {
38+
expect(dedupe(input)).toEqual(expected);
39+
});
40+
});
2441

2542
// Given an array of strings or numbers
2643
// When passed to the dedupe function
27-
// Then it should return a new array with duplicates removed while preserving the
44+
// Then it should return a new array with duplicates removed while preserving the
2845
// first occurrence of each element from the original array.
46+
const testCaseWithDuplicates = [
47+
{ input: [1, 2, 2, 3, 1], expected: [1, 2, 3] },
48+
{ input: ["a", "b", "a", "c", "b"], expected: ["a", "b", "c"] },
49+
{ input: [5, 5, 1, 1, 2, 3, 2], expected: [5, 1, 2, 3] },
50+
{
51+
input: ["apple", "banana", "apple", "orange"],
52+
expected: ["apple", "banana", "orange"],
53+
},
54+
{ input: [1, "a", 1, "b", "a", 2], expected: [1, "a", "b", 2] },
55+
{ input: ["hey", "hey", "hey", "hey", "hey", "hey"], expected: ["hey"] },
56+
];
57+
testCaseWithDuplicates.forEach(({ input, expected }) => {
58+
test("given an array with duplicates, return a new array with only the first occurrence", () => {
59+
expect(dedupe(input)).toEqual(expected);
60+
});
61+
});

‎Sprint-1/package-lock.json‎

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Sprint-1/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"author": "",
1111
"license": "ISC",
1212
"devDependencies": {
13-
"jest": "^29.7.0"
13+
"jest": "^29.7.0",
14+
"prettier": "3.9.6"
1415
}
1516
}

0 commit comments

Comments
 (0)