Skip to content
Open
21 changes: 18 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,24 @@
// 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;
if (!Array.isArray(list) || list.length === 0) {
return null;
}
const numbersOnly = list.filter((element) => typeof element === "number");
if (numbersOnly.length === 0) {
return null;
}

const sortedList = [...numbersOnly].sort((a, b) => a - b);

if (sortedList.length % 2 === 0) {
const middleIndex = Math.floor(sortedList.length / 2);
return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
}
const middleIndex = Math.floor(sortedList.length / 2);

return sortedList[middleIndex];
}


module.exports = calculateMedian;
28 changes: 23 additions & 5 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe("calculateMedian", () => {
{ input: [1, 2, 3, 4], expected: 2.5 },
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5 },
].forEach(({ input, expected }) =>
it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
it(`returns the median for [${input}]`, () =>
expect(calculateMedian(input)).toEqual(expected))
);

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

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

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

[
Expand All @@ -45,6 +56,13 @@ describe("calculateMedian", () => {
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
].forEach(({ input, expected }) =>
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
it(`filters out non-numeric values and calculates the median for [${input}]`, () =>
expect(calculateMedian(input)).toEqual(expected))
);

it("calculates the correct median for salaries array", () => {
const salaries = [10, 40, 50, 70, 90];
const median = calculateMedian(salaries);
expect(median).toEqual(50);
});
});
10 changes: 9 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
function dedupe() {}
function dedupe(list) {
if (!Array.isArray(list)) {
return [];
}

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

module.exports = dedupe;
29 changes: 27 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,38 @@ E.g. dedupe([1, 2, 1]) returns [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
// Then it should return a copy of the original array

// Given an array of strings or numbers
// When passed to the dedupe function
// Then it should return a new array with duplicates removed while preserving the
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.

describe("dedupe function", () => {
test("should remove duplicate elements from an array", () => {
expect(dedupe([1, 2, 2, 3, 1, 4])).toEqual([1, 2, 3, 4]);
expect(dedupe(["apple", "banana", "apple", "orange"])).toEqual([
"apple",
"banana",
"orange",
]);
});

test("should return the same array if there are no duplicates", () => {
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
});

test("should return an empty array if given an empty array", () => {
expect(dedupe([])).toEqual([]);
});

test("should return an empty array if given invalid input (non-arrays)", () => {
expect(dedupe(null)).toEqual([]);
expect(dedupe(undefined)).toEqual([]);
expect(dedupe("not an array")).toEqual([]);
});
});
14 changes: 14 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
function findMax(elements) {
if (!Array.isArray(elements) || elements.length === 0) {
return -Infinity;
}
let max = -Infinity;

for (const item of elements) {
if (typeof item === "number" && !Number.isNaN(item)) {
if (item > max) {
max = item;
}
}
}

return max;
}

module.exports = findMax;
32 changes: 32 additions & 0 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,35 @@ 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

describe("findMax function", () => {
test("should return the maximum number in an array of positive numbers", () => {
expect(findMax([1, 5, 3, 9, 2])).toBe(9);
});

test("should return the maximum number in an array with negative numbers", () => {
expect(findMax([-10, -3, -50, -1])).toBe(-1);
});

test("should handle arrays with mixed types and ignore non-numbers", () => {
expect(findMax([1, "apple", 5, null, true, 3])).toBe(5);
});

test("should ignore NaN values", () => {
expect(findMax([1, NaN, 10, 2])).toBe(10);
});

test("should return -Infinity if given an empty array", () => {
expect(findMax([])).toBe(-Infinity);
});

test("should return -Infinity if given an array with no valid numbers", () => {
expect(findMax(["a", "b", null, NaN])).toBe(-Infinity);
});

test("should return -Infinity for non-array inputs", () => {
expect(findMax(null)).toBe(-Infinity);
expect(findMax(undefined)).toBe(-Infinity);
expect(findMax("hello")).toBe(-Infinity);
});
});
10 changes: 10 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
function sum(elements) {
if (!Array.isArray(elements) || elements.length === 0) {
return 0;
}

const numbersOnly = elements.filter(
(item) => typeof item === "number" && !isNaN(item)
);


return numbersOnly.reduce((acc, curr) => acc + curr, 0);
}

module.exports = sum;
41 changes: 40 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,42 @@ 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

describe("sum function", () => {
// Given an empty array
test("given an empty array, returns 0", () => {
expect(sum([])).toBe(0);
});

// Given an array with just one number
test("given an array with just one number, returns that number", () => {
expect(sum([42])).toBe(42);
});

// Given an array containing negative numbers
test("given an array containing negative numbers, returns the correct total sum", () => {
expect(sum([10, -5, 20, -15])).toBe(10);
});

// Given an array with decimal/float numbers
test("given an array with decimal/float numbers, returns the correct total sum", () => {
expect(sum([1.5, 2.25, 3.25])).toBe(7);
});

// Given an array containing non-number values
test("given an array containing non-number values, ignores non-numerical values", () => {
expect(sum(["hey", 10, "hi", 60, 10])).toBe(80);
expect(sum([10, true, null, undefined, NaN, 20])).toBe(30);
});

// Given an array with only non-number values
test("given an array with only non-number values, returns 0", () => {
expect(sum(["apple", "banana", true, null])).toBe(0);
});

// Edge case: Non-array inputs
test("given non-array inputs, returns 0", () => {
expect(sum(null)).toBe(0);
expect(sum("not an array")).toBe(0);
});
});
Loading