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 @@ -4,11 +4,36 @@

// 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) {
list = list.filter((item) => typeof item === "number" && !isNaN(item));

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

list.sort((a, b) => a - b);

const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;

if (list.length % 2 === 1) {
return list[middleIndex];
}

return (list[middleIndex - 1] + list[middleIndex]) / 2;
}

module.exports = calculateMedian;

// function calculateMedian(list) {
// if (list.length === 0) return null;
// list = list.filter((item) => typeof item === "number" && !isNaN(item));
// list.sort((a, b) => a - b);
// const middleIndex = Math.floor(list.length / 2);
// if (list.length % 2 === 1) {
// return list[middleIndex]; //odd number if elements
// }

// return (list[middleIndex - 1] + list[middleIndex]) / 2;
// }

// module.exports = calculateMedian;
117 changes: 77 additions & 40 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,82 @@

const calculateMedian = require("./median.js");

// describe("calculateMedian", () => {
// [
// { input: [1, 2, 3], expected: 2 },
// { input: [1, 2, 3, 4, 5], expected: 3 },
// { 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))
// );

// [
// { input: [3, 1, 2], expected: 2 },
// { input: [5, 1, 3, 4, 2], expected: 3 },
// { input: [4, 2, 1, 3], expected: 2.5 },
// { input: [6, 1, 5, 3, 2, 4], expected: 3.5 },
// { 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("doesn't modify the input array [3, 1, 2]", () => {
// const list = [3, 1, 2];
// calculateMedian(list);
// 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))
// );

// [
// { input: [1, 2, "3", null, undefined, 4], expected: 2 },
// { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 },
// { input: [1, "2", 3, "4", 5], expected: 3 },
// { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 },
// { 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))
// );
// });

describe("calculateMedian", () => {
[
{ input: [1, 2, 3], expected: 2 },
{ input: [1, 2, 3, 4, 5], expected: 3 },
{ 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))
);

[
{ input: [3, 1, 2], expected: 2 },
{ input: [5, 1, 3, 4, 2], expected: 3 },
{ input: [4, 2, 1, 3], expected: 2.5 },
{ input: [6, 1, 5, 3, 2, 4], expected: 3.5 },
{ 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("doesn't modify the input array [3, 1, 2]", () => {
const list = [3, 1, 2];
calculateMedian(list);
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))
);

[
{ input: [1, 2, "3", null, undefined, 4], expected: 2 },
{ input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 },
{ input: [1, "2", 3, "4", 5], expected: 3 },
{ input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 },
{ 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))
);
test("returns null for an empty array", () => {
expect(calculateMedian([])).toBeNull();
});

test("returns null when there are no numbers", () => {
expect(calculateMedian(["a", "b", null])).toBeNull();
});

test("returns the median for an odd number of elements", () => {
expect(calculateMedian([1, 2, 3])).toEqual(2);
expect(calculateMedian([5, 1, 3])).toEqual(3);
});

test("returns the median for an even number of elements", () => {
expect(calculateMedian([1, 2, 3, 4])).toEqual(2.5);
expect(calculateMedian([6, 2, 4, 8])).toEqual(5);
});

test("ignores non-number values", () => {
expect(calculateMedian([1, "a", 2, 3])).toEqual(2);
expect(calculateMedian([5, "x", 1, 3])).toEqual(3);
});

test("ignores NaN values", () => {
expect(calculateMedian([1, NaN, 2, 3])).toEqual(2);
});

test("returns the single number when the array has one number", () => {
expect(calculateMedian([7])).toEqual(7);
});

test("returns the single number when mixed with non-numbers", () => {
expect(calculateMedian(["a", 7, null])).toEqual(7);
});
});
6 changes: 5 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
function dedupe() {}
function dedupe(arr) {
return [...new Set(arr)];
Comment thread
mandipsanger marked this conversation as resolved.
}

module.exports = dedupe;
37 changes: 32 additions & 5 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const dedupe = require("./dedupe.js");
/*
Dedupe Array

Expand All @@ -16,13 +15,41 @@ 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");

const dedupe = require("./dedupe");

// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
test("should return an empty array when given an empty array", () => {
expect(dedupe([])).toEqual([]);
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("should return a copy of the original array when there are no duplicates", () => {
expect(dedupe([1, 2, 3, 4])).toEqual([1, 2, 3, 4]);
});

// Given an array of strings with duplicates
// When passed to the dedupe function
// Then it should remove duplicate strings while preserving the first occurrence
test("should remove duplicate strings while preserving the first occurrence", () => {
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
});

// Given an array of numbers with duplicates
// When passed to the dedupe function
// Then it should remove duplicate numbers while preserving the first occurrence
test("should remove duplicate numbers while preserving the first occurrence", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});

// Given an array of strings or numbers
// Given an array with one element
// When passed to the dedupe function
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
// Then it should return the same array
test("should return the same array when it contains only one element", () => {
expect(dedupe([42])).toEqual([42]);
});
9 changes: 9 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function findMax(elements) {
const numbersOnly = elements.filter(
(item) => typeof item === "number" && !Number.isNaN(item)
);
if (numbersOnly.length === 0) {
return -Infinity;
}
const sorted = [...numbersOnly].sort((a, b) => a - b);
const max = sorted[sorted.length - 1];
Comment thread
mandipsanger marked this conversation as resolved.
return max;
}

module.exports = findMax;
64 changes: 51 additions & 13 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,81 @@
/* Find the maximum element of an array of numbers
/* Find the findMaximum 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.

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)
E.g. findMax([30, 50, 10, 40]), target output: 50
E.g. findMax(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements)

You should implement this function in max.js, and add tests for it in this file.
You should implement this function in findMax.js, and add tests for it in this file.

We have set things up already so that this file can see your function from the other file.
*/

const findMax = require("./max.js");

// Given an empty array
// When passed to the max function
// When passed to the findMax 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("returns -Infinity for an empty array", () => {
const currentOutput = findMax([]);
const targetOutput = -Infinity;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with one number
// When passed to the max function
// When passed to the findMax function
// Then it should return that number

test("returns with one number", () => {
const currentOutput = findMax([3]);
const targetOutput = 3;
expect(currentOutput).toEqual(targetOutput);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// When passed to the findMax function
// Then it should return the largest number overall
test("returns largest number", () => {
const currentOutput = findMax([3, -10]);
const targetOutput = 3;
expect(currentOutput).toEqual(targetOutput);
});

// Given an array with just negative numbers
// When passed to the max function
// When passed to the findMax function
// Then it should return the closest one to zero
test("return closest to 0", () => {
const currentOutput = findMax([-7, -3, -10]);
const targetOutput = -3;
expect(currentOutput).toEqual(targetOutput);
});

// Given an array with decimal numbers
// When passed to the max function
// When passed to the findMax function
// Then it should return the largest decimal number
test("return to largest decimal number", () => {
const currentOutput = findMax([2.5, 5.6]);
const targetOutput = 5.6;
expect(currentOutput).toEqual(targetOutput);
});

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
// When passed to the findMax function
// Then it should return the findMax and ignore non-numeric values

test("return to findMax and ignore non-numeric values", () => {
const currentOutput = findMax([7, 4, -10, "b", "f"]);
const targetOutput = 7;
expect(currentOutput).toEqual(targetOutput);
});

// Given an array with only non-number values
// When passed to the max function
// When passed to the findMax function
// Then it should return the least surprising value given how it behaves for all other inputs
test("return the least surprising value", () => {
const currentOutput = findMax([NaN, "hi", true]);
const targetOutput = -Infinity;
expect(currentOutput).toEqual(targetOutput);
});
9 changes: 9 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function sum(elements) {
let total = 0;

for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number" && !isNaN(elements[i])) {
total += elements[i];
}
}

return total;
}

module.exports = sum;
Loading
Loading