Skip to content
23 changes: 20 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,26 @@
// or 'list' has mixed values (the function is expected to sort only numbers).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
// Must be an array with at least one number
if (!Array.isArray(list)) return null;

// Filter only valid numbers
const numbers = list.filter((value) => typeof value === "number" && !isNaN(value));

// No numeric values - null
if (numbers.length === 0) return null;

// Sort without mutating original input
const sorted = [...numbers].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);

// Odd length
if (sorted.length % 2 !== 0) {
return sorted[mid];
}

// Even length
return (sorted[mid - 1] + sorted[mid]) / 2;
}

module.exports = calculateMedian;
8 changes: 7 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
function dedupe() {}
function dedupe(arr) {
if (!Array.isArray(arr)) return [];

return [...new Set(arr)];
}

module.exports = dedupe;
6 changes: 5 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
test.todo("given an empty array, it returns an empty array");
// test.todo("given an empty array, it returns an empty array");

// Given an array with no duplicates
// When passed to the dedupe function
Expand All @@ -25,3 +25,7 @@ test.todo("given an empty array, it returns an empty array");
// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurence of each element

it ("given an empty array, it returns an empty array", () => {
expect(dedupe([])).toEqual([]);
});
7 changes: 7 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function findMax(elements) {
if (!Array.isArray(elements)) return null;

const numbers = elements.filter((value) => typeof value === "number" && !isNaN(value));

if (numbers.length === 0) return null;

return Math.max(...numbers);
}

module.exports = findMax;
6 changes: 5 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const findMax = require("./max.js");
// When passed to the max function
// Then it should return -Infinity
// Delete this test.todo and replace it with a test.
test.todo("given an empty array, returns -Infinity");
// test.todo("given an empty array, returns -Infinity");

// Given an array with one number
// When passed to the max function
Expand All @@ -41,3 +41,7 @@ test.todo("given an empty array, returns -Infinity");
// Given an array with only non-number values
// When passed to the max function
// Then it should return the least surprising value given how it behaves for all other inputs

it("given an empty array, returns null", () => {
expect(findMax([])).toBe(null);
});
8 changes: 8 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function sum(elements) {
if (!Array.isArray(elements)) return 0;

return elements.reduce((total, value) => {
if (typeof value === "number" && !isNaN(value)) {
return total + value;
}
return total;
}, 0);
}

module.exports = sum;
5 changes: 4 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const sum = require("./sum.js");
// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")
// test.todo("given an empty array, returns 0")

// Given an array with just one number
// When passed to the sum function
Expand All @@ -34,3 +34,6 @@ test.todo("given an empty array, returns 0")
// Given an array with only non-number values
// When passed to the sum function
// Then it should return the least surprising value given how it behaves for all other inputs
it ("given an empty array, returns 0", () => {
expect(sum([])).toBe(0);
});
Loading