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
155 changes: 62 additions & 93 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,113 +6,82 @@

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

describe("calculateMedian", () => {
it("returns the median for [1, 2, 3]", () => {
expect(calculateMedian([1, 2, 3])).toEqual(2);
});

it("returns the median for [1, 2, 3, 4, 5]", () => {
expect(calculateMedian([1, 2, 3, 4, 5])).toEqual(3);
});

it("returns the median for [1, 2, 3, 4]", () => {
expect(calculateMedian([1, 2, 3, 4])).toEqual(2.5);
});

it("returns the median for [1, 2, 3, 4, 5, 6]", () => {
expect(calculateMedian([1, 2, 3, 4, 5, 6])).toEqual(3.5);
});

it("returns the correct median for unsorted array [3, 1, 2]", () => {
expect(calculateMedian([3, 1, 2])).toEqual(2);
});

it("returns the correct median for unsorted array [5, 1, 3, 4, 2]", () => {
expect(calculateMedian([5, 1, 3, 4, 2])).toEqual(3);
});

it("returns the correct median for unsorted array [4, 2, 1, 3]", () => {
expect(calculateMedian([4, 2, 1, 3])).toEqual(2.5);
});
// 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))
// );
// });

it("returns the correct median for unsorted array [6, 1, 5, 3, 2, 4]", () => {
expect(calculateMedian([6, 1, 5, 3, 2, 4])).toEqual(3.5);
});

it("returns the correct median for unsorted array [110, 20, 0]", () => {
expect(calculateMedian([110, 20, 0])).toEqual(20);
});

it("returns the correct median for unsorted array [6, -2, 2, 12, 14]", () => {
expect(calculateMedian([6, -2, 2, 12, 14])).toEqual(6);
});

it("doesn't modify the input array [3, 1, 2]", () => {
const list = [3, 1, 2];
calculateMedian(list);
expect(list).toEqual([3, 1, 2]);
});

// There is no median of an empty array, so calculateMedian should throw
it("throws when given an empty array", () => {
expect(() => calculateMedian([])).toThrow(
new Error("calculateMedian requires a non-empty array")
);
});

// Input that isn't an array should throw
it("throws when given a string", () => {
expect(() => calculateMedian("banana")).toThrow(
new Error("calculateMedian requires an array of numbers")
);
});

it("throws when given a number", () => {
expect(() => calculateMedian(123)).toThrow(
new Error("calculateMedian requires an array of numbers")
);
describe("calculateMedian", () => {
test("returns null for an empty array", () => {
expect(calculateMedian([])).toBeNull();
});

it("throws when given null", () => {
expect(() => calculateMedian(null)).toThrow(
new Error("calculateMedian requires an array of numbers")
);
test("returns null when there are no numbers", () => {
expect(calculateMedian(["a", "b", null])).toBeNull();
});

it("throws when given an object", () => {
expect(() => calculateMedian({})).toThrow(
new Error("calculateMedian requires an array of numbers")
);
test("returns the median for an odd number of elements", () => {
expect(calculateMedian([1, 2, 3])).toEqual(2);
expect(calculateMedian([5, 1, 3])).toEqual(3);
});

it("throws when called with no argument", () => {
expect(() => calculateMedian()).toThrow(
new Error("calculateMedian requires an array of numbers")
);
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);
});

// Arrays containing any non-number value should throw, rather than filtering them out
it("throws for an array of strings", () => {
expect(() => calculateMedian(["ten", "twenty", "thirty"])).toThrow(
new Error("calculateMedian requires an array of numbers")
);
test("ignores non-number values", () => {
expect(calculateMedian([1, "a", 2, 3])).toEqual(2);
expect(calculateMedian([5, "x", 1, 3])).toEqual(3);
});

it("throws for an array mixing numbers and strings", () => {
expect(() => calculateMedian([1, "2", 3, "4", 5])).toThrow(
new Error("calculateMedian requires an array of numbers")
);
test("ignores NaN values", () => {
expect(calculateMedian([1, NaN, 2, 3])).toEqual(2);
});

it("throws for an array containing null", () => {
expect(() => calculateMedian([1, 2, null, 4])).toThrow(
new Error("calculateMedian requires an array of numbers")
);
test("returns the single number when the array has one number", () => {
expect(calculateMedian([7])).toEqual(7);
});

it("throws for an array containing undefined", () => {
expect(() => calculateMedian([3, 1, undefined, 2])).toThrow(
new Error("calculateMedian requires an array of numbers")
);
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]);
});
11 changes: 10 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function findMax(elements) {
function findMax(list) {
if (!Array.isArray(list)) {
throw new Error("findMax requires an array of numbers");
}

if (!list.every((item) => typeof item === "number" && !Number.isNaN(item))) {
throw new Error("findMax requires an array of numbers");
}

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

module.exports = findMax;
74 changes: 60 additions & 14 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,89 @@
/* 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]) throws Error("findMax requires an array of numbers") (max can't compare non-numerical elements, so it shouldn't guess)
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 the number when the array contains 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 the 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("returns the number closest to zero", () => {
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("returns the largest decimal number", () => {
const currentOutput = findMax([2.5, 5.6]);
const targetOutput = 5.6;

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

// Given an array containing a value that isn't a number
// When passed to the max function
// When passed to the findMax function
// Then it should throw Error("findMax requires an array of numbers")
test("throws an error when the array contains a non-number", () => {
expect(() => findMax(["hey", 10, "hi", 60, 10])).toThrow(
new Error("findMax requires an array of numbers")
);
});

// Given something that isn't an array at all, such as "hey", 42 or no argument
// When passed to the max function
// Given something that isn't an array at all
// When passed to the findMax function
// Then it should throw Error("findMax requires an array of numbers")
test("throws an error when the input is not an array", () => {
expect(() => findMax("hey")).toThrow(
new Error("findMax requires an array of numbers")
);

expect(() => findMax(42)).toThrow(
new Error("findMax requires an array of numbers")
);

expect(() => findMax()).toThrow(
new Error("findMax requires an array of numbers")
);
});
11 changes: 10 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function sum(elements) {
function sum(list) {
if (!Array.isArray(list)) {
throw new Error("sum requires an array of numbers");
}

if (!list.every((item) => typeof item === "number" && !Number.isNaN(item))) {
throw new Error("sum requires an array of numbers");
}

return list.reduce((total, number) => total + number, 0);
}

module.exports = sum;
Loading