Skip to content

Commit 258be2b

Browse files
Fix median and improve implement tests
1 parent bddbceb commit 258be2b

4 files changed

Lines changed: 28 additions & 10 deletions

File tree

Sprint-1/fix/median.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@
66
// or 'list' has mixed values (the function is expected to sort only numbers).
77

88
function calculateMedian(list) {
9-
if(!Array.isArray(list)){return null;}
10-
const number = list.filter(n => typeof n === "number");
11-
if(number.length === 0) {return null;}
12-
number.sort((a,b) => a-b)
13-
const middleIndex = Math.floor(number.length / 2);
9+
if (!Array.isArray(list)) {
10+
return null;
11+
}
1412

13+
const numbers = list.filter(
14+
(item) => typeof item === "number" && Number.isFinite(item)
15+
);
1516

16-
if(number.length%2===0){return (number[middleIndex-1] + number[middleIndex])/2;
17+
if (numbers.length === 0) {
18+
return null;
1719
}
18-
else return number[middleIndex];
19-
}
2020

21+
const sortedNumbers = [...numbers].sort((a, b) => a - b);
22+
const middleIndex = Math.floor(sortedNumbers.length / 2);
2123

24+
if (sortedNumbers.length % 2 === 0) {
25+
return (sortedNumbers[middleIndex - 1] + sortedNumbers[middleIndex]) / 2;
26+
}
27+
28+
return sortedNumbers[middleIndex];
29+
}
2230

23-
module.exports = calculateMedian
31+
module.exports = calculateMedian;

Sprint-1/implement/dedupe.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test("given an array with no duplication, it copy of the original array ", () =>
3131
const currentOutput = dedupe(array);
3232
const targetOutput = [1, 2, 3];
3333
expect(currentOutput).toEqual(targetOutput);
34+
expect(currentOutput).not.toBe(array);
3435
});
3536
// Given an array with strings or numbers
3637
// When passed to the dedupe function

Sprint-1/implement/max.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ test("given an array with one non-number return the max and ignore non-numeric v
6969
const targetOutput = 10;
7070
expect(currentOutput).toEqual(targetOutput);
7171
});
72+
// Given an array with numeric string values
73+
// When passed to the max function
74+
// Then it should ignore numeric strings and only consider real numbers
75+
test("ignores numeric strings like '300'", () => {
76+
const maxElement = ["300", 10, 20];
77+
const currentOutput = findMax(maxElement);
78+
const targetOutput = 20;
79+
expect(currentOutput).toEqual(targetOutput);
80+
});
7281
// Given an array with only non-number values
7382
// When passed to the max function
7483
// Then it should return the least surprising value given how it behaves for all other inputs

Sprint-1/implement/sum.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test("given an array with decimal/float numbers [1.5, -2.4, 4.5], returns the co
4747
const list = [1.5, -2.4, 4.5];
4848
const currentOutput = sum(list);
4949
const targetOutput = 3.6;
50-
expect(currentOutput).toEqual(targetOutput);
50+
expect(currentOutput).toBeCloseTo(targetOutput);
5151
});
5252

5353
// Given an array containing non-number values

0 commit comments

Comments
 (0)