Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,35 @@
// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
// 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;
// }

// module.exports = calculateMedian;

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 numbers = list.filter((item) => Number.isFinite(item));

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

// Copy before sort (extra safety, though filter already creates new array)
const sorted = [...numbers].sort((a, b) => a - b);
Comment on lines +27 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this "extra safety" to make a copy of numbers before sorting? Safety from what?


const middle = Math.floor(sorted.length / 2);

if (sorted.length % 2 === 0) {
return (sorted[middle - 1] + sorted[middle]) / 2;
}

return sorted[middle];
}

module.exports = calculateMedian;
48 changes: 43 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,33 @@ 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))
);
});

describe("calculateMedian - ignores non-finite numbers", () => {
[
{ input: [1, 2, NaN, 3], expected: 2 },
{ input: [1, 2, Infinity, 3], expected: 2 },
{ input: [1, 2, -Infinity, 3], expected: 2 },
{ input: [1, 2, NaN, Infinity, -Infinity, 3], expected: 2 },
{ input: [NaN, Infinity, -Infinity, 5, 10], expected: 7.5 },
].forEach(({ input, expected }) =>
it(`ignores NaN/Infinity values in [${input}]`, () => {
expect(calculateMedian(input)).toEqual(expected);
})
);

it("returns null when all values are non-finite", () => {
expect(calculateMedian([NaN, Infinity, -Infinity])).toBe(null);
});

it("handles mix of finite and non-finite with even count", () => {
expect(calculateMedian([1, 2, 3, 4, Infinity, NaN])).toBe(2.5);
});

it("handles mix of finite and non-finite with odd count", () => {
expect(calculateMedian([1, 2, 3, Infinity, NaN])).toBe(2);
});
});
7 changes: 6 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
function dedupe() {}
function dedupe(arr) {
if (arr.length === 0) return [];
return [...new Set(arr)];
}

module.exports = dedupe;
63 changes: 43 additions & 20 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
const dedupe = require("./dedupe.js");
/*
Dedupe Array
const dedupe = require("./dedupe");

📖 Dedupe means **deduplicate**
describe("dedupe", () => {
// 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("given an empty array, it returns an empty array", () => {
const result = dedupe([]);
expect(result).toEqual([]);
});

In this kata, you will need to deduplicate the elements of an array
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("given an array with no duplicates, it returns a copy of the original array", () => {
const input = [1, 2, 3, 4];
const result = dedupe(input);

E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c']
E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
E.g. dedupe([1, 2, 1]) target output: [1, 2]
*/
expect(result).toEqual([1, 2, 3, 4]);
expect(result).not.toBe(input); // should be a new copy
});

// Acceptance Criteria:
// Given an array with strings
// When passed to the dedupe function
// Then it should remove duplicates preserving first occurrence
test("removes duplicate strings preserving first occurrence", () => {
const input = ["a", "a", "a", "b", "b", "c"];
const result = dedupe(input);

// 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");
expect(result).toEqual(["a", "b", "c"]);
});

// 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 with numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurence of each element
test("removes duplicate numbers preserving first occurrence", () => {
const input = [5, 1, 1, 2, 3, 2, 5, 8];
const result = dedupe(input);

// 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
expect(result).toEqual([5, 1, 2, 3, 8]);
});

test("removes duplicates in mixed order", () => {
const input = [1, 2, 1];
const result = dedupe(input);

expect(result).toEqual([1, 2]);
});
});
19 changes: 19 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
function findMax(elements) {
if (!Array.isArray(elements)) {
return -Infinity;
}

const numbers = elements.filter((el) => Number.isFinite(el));

if (numbers.length === 0) {
return -Infinity;
}

let max = numbers[0];

for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}

return max;
}

module.exports = findMax;
89 changes: 57 additions & 32 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,68 @@
/* Find the maximum element of an array of numbers

In this kata, you will need to implement a function that find the largest numerical element of an array.
const findMax = require("./max.js");

E.g. max([30, 50, 10, 40]), target output: 50
E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements)
describe("findMax", () => {
// Given an empty array
// When passed to the max function
// Then it should return -Infinity
test("given an empty array, returns -Infinity", () => {
expect(findMax([])).toBe(-Infinity);
});

You should implement this function in max.js, and add tests for it in this file.
// Given an array with one number
// When passed to the max function
// Then it should return that number
test("given an array with one number, returns that number", () => {
expect(findMax([5])).toBe(5);
});

We have set things up already so that this file can see your function from the other file.
*/
// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test("returns the largest number from positive and negative numbers", () => {
expect(findMax([-10, 20, -5, 15])).toBe(20);
});

const findMax = require("./max.js");
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("returns the largest number among negative numbers", () => {
expect(findMax([-10, -3, -50, -1])).toBe(-1);
});

// Given an empty array
// 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");
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("returns the largest decimal number", () => {
expect(findMax([1.2, 3.7, 2.5, 3.6])).toBe(3.7);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
test("ignores non-number values (including numeric strings)", () => {
expect(findMax(["hey", 10, "300", 60, 10])).toBe(60);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
// 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
test("returns -Infinity when array has only non-number values", () => {
expect(findMax(["a", "b", "c"])).toBe(-Infinity);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("does not treat numeric strings as numbers", () => {
expect(findMax([20, "300"])).toBe(20);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("returns -Infinity when only numeric strings are present", () => {
expect(findMax(["100", "200", "300"])).toBe(-Infinity);
});

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
test("ignores NaN and Infinity values", () => {
expect(findMax([10, NaN, 50, Infinity, -Infinity])).toBe(50);
});

// 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
test("returns -Infinity when only NaN and Infinity are present", () => {
expect(findMax([NaN, Infinity, -Infinity])).toBe(-Infinity);
});
});
3 changes: 3 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function sum(elements) {
const numbers = elements.filter((el) => Number.isFinite(el));
const sumNumbers = numbers.reduce((a, b) => a + b, 0);
return sumNumbers;
}

module.exports = sum;
Loading
Loading