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
26 changes: 16 additions & 10 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// Fix this implementation
// Start by running the tests for this function
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory
function calculateMedian(list) {
if (
!Array.isArray(list) ||
list.length === 0 ||
list.some((item) => typeof item !== "number" || Number.isNaN(item))
) {
throw new Error("calculateMedian requires an array of numbers");
}

// Hint: Please consider scenarios when 'list' isn't an array, is empty,
// or contains values that aren't numbers (the function is expected to throw - see the tests).
const sorted = [...list].sort((a, b) => a - b);
const middleIndex = Math.floor(sorted.length / 2);

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
if (sorted.length % 2 === 0) {
return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
}

return sorted[middleIndex];
}

module.exports = calculateMedian;
module.exports = calculateMedian;
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(elements) {
return [...new Set(elements)];
}

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

📖 Dedupe means **deduplicate**
describe("dedupe", () => {
test("given an empty array, it returns an empty array", () => {
expect(dedupe([])).toEqual([]);
});

In this kata, you will need to deduplicate the elements of an array
test("given an array with no duplicates, it returns a copy of the original array", () => {
const input = [1, 2, 3];
const result = dedupe(input);
expect(result).toEqual([1, 2, 3]);
expect(result).not.toBe(input);
});

E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c']
E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8]
E.g. dedupe([1, 2, 1]) returns [1, 2]
*/

// Acceptance Criteria:

// 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");

// 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
// first occurrence of each element from the original array.
test("given an array of strings or numbers, it returns a new array with duplicates removed while preserving the first occurrence", () => {
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
});
15 changes: 9 additions & 6 deletions Sprint-1/implement/describe-median.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// Don't change this function.
function calculateMedian(list) {
if (!Array.isArray(list)) {
throw new Error("calculateMedian requires an array of numbers");
}
if (list.length === 0) {
throw new Error("calculateMedian requires a non-empty array");
}
Expand All @@ -20,6 +16,13 @@ function calculateMedian(list) {
}

// Implement this function. See describe-median.test.js for the acceptance criteria.
function describeMedian(list) {}
function describeMedian(list) {
try {
const median = calculateMedian(list);
return `The median is ${median}`;
} catch (error) {
return `Could not calculate a median: ${error.message}`;
}
}

module.exports = describeMedian;
module.exports = describeMedian;
50 changes: 20 additions & 30 deletions Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
/* Describe the median, or explain why there isn't one

calculateMedian is given to you at the top of describe-median.js. It throws
when it can't calculate a median. Don't change it.

Implement describeMedian, which calls calculateMedian and returns a sentence
instead of crashing.

E.g. describeMedian([1, 2, 3]), target output: "The median is 2"
E.g. describeMedian([]), target output: "Could not calculate a median: calculateMedian requires a non-empty array"

You'll need a try/catch block for this. The catch block must use the error's
message, so the caller can see what went wrong.
*/

const describeMedian = require("./describe-median.js");

// Acceptance criteria:

// Given an array of numbers
// When passed to describeMedian
// Then it should return "The median is " followed by the median
// Delete this test.todo and replace it with a test.
test.todo('given [1, 2, 3], returns "The median is 2"');

// Given an empty array
// When passed to describeMedian
// Then it should return "Could not calculate a median: calculateMedian requires a non-empty array"

// Given something that isn't an array of numbers, e.g. "banana"
// When passed to describeMedian
// Then it should return "Could not calculate a median: calculateMedian requires an array of numbers"
describe("describeMedian", () => {
test('given [1, 2, 3], returns "The median is 2"', () => {
expect(describeMedian([1, 2, 3])).toBe("The median is 2");
});

test("given an empty array, returns an explanation using the error message", () => {
expect(describeMedian([])).toBe(
"Could not calculate a median: calculateMedian requires a non-empty array"
);
});

@hackertainment hackertainment Nov 23, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Have you actually run this test case? I cannot see how this test passed. Since the first line of "Sprint-1/implement/describe-median.js" // Don't change this function. and the first few lines in the function calculateMedian() has been deleted (maybe accidentally), you may need to restore only the function (but keeping your code in describeMedian() untouched). This test case should be kept as well. If you don't know how to fix this safely, please feel free to ask a volunteer in Saturday workshop. Thank you.


test("given something that isn't an array of numbers, returns an explanation using the error message", () => {
expect(describeMedian("banana")).toBe(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
expect(describeMedian([1, "two", 3])).toBe(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
});
});
10 changes: 9 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function findMax(elements) {
if (
!Array.isArray(elements) ||
elements.some((item) => typeof item !== "number" || Number.isNaN(item))
) {
throw new Error("findMax requires an array of numbers");
}

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

module.exports = findMax;
module.exports = findMax;
80 changes: 39 additions & 41 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
/* 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.

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)

You should implement this function in max.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
// 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 one number
// When passed to the max function
// Then it should return that number

// 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 just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

// Given an array containing a value that isn't a number
// When passed to the max function
// Then it should throw 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
// Then it should throw Error("findMax requires an array of numbers")
describe("findMax", () => {
test("given an empty array, returns -Infinity", () => {
expect(findMax([])).toBe(-Infinity);
});

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

test("given an array with both positive and negative numbers, returns the largest number overall", () => {
expect(findMax([-10, 50, 0, -25, 30])).toBe(50);
});

test("given an array with just negative numbers, returns the closest one to zero", () => {
expect(findMax([-30, -5, -100, -12])).toBe(-5);
});

test("given an array with decimal numbers, returns the largest decimal number", () => {
expect(findMax([1.5, 3.75, 2.1, 3.74])).toBe(3.75);
});

test("given an array containing a value that isn't a number, throws an Error", () => {
expect(() => findMax(["hey", 10, "hi", 60, 10])).toThrow(
new Error("findMax requires an array of numbers")
);
});

test("given something that isn't an array at all, throws an Error", () => {
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")
);
});
});
19 changes: 17 additions & 2 deletions Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function calculateMean(list) {}
function calculateMean(list) {
if (!Array.isArray(list)) {
throw new Error("calculateMean requires an array of numbers");
}

module.exports = calculateMean;
if (list.length === 0) {
throw new Error("calculateMean requires a non-empty array");
}

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

const total = list.reduce((sum, num) => sum + num, 0);
return total / list.length;
}

module.exports = calculateMean;
95 changes: 47 additions & 48 deletions Sprint-1/implement/mean.test.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
/* Calculate the mean of an array of numbers

In this kata, you will need to implement a function that calculates the mean of an array of numbers.

E.g. calculateMean([1, 2, 6]), target output: 3
E.g. calculateMean([]) throws Error("calculateMean requires a non-empty array")
E.g. calculateMean("banana") throws Error("calculateMean requires an array of numbers")

There is no mean of an empty array, and a string isn't an array of numbers, so
calculateMean should throw rather than return a value. This is the same problem
calculateMedian solves in the prep and in the fix directory.

You should implement this function in mean.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 calculateMean = require("./mean.js");

// Acceptance criteria:

// Given an array of numbers
// When passed to calculateMean
// Then it should return their mean
// Delete this test.todo and replace it with a test.
test.todo("given [1, 2, 6], returns 3");

// Given an array with a single number
// When passed to calculateMean
// Then it should return that number

// Given an array containing negative or decimal numbers
// When passed to calculateMean
// Then it should return the correct mean

// Given an empty array
// When passed to calculateMean
// Then it should throw Error("calculateMean requires a non-empty array")

// Given a value that isn't an array, e.g. "banana", 42, null or {}
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")

// Given no argument at all
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")

// Given an array containing a non-number value, e.g. [1, "2", 3]
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")
describe("calculateMean", () => {
test("given an array of numbers, returns their mean", () => {
expect(calculateMean([1, 2, 6])).toBe(3);
});

test("given an array with a single number, returns that number", () => {
expect(calculateMean([10])).toBe(10);
});

test("given an array containing negative or decimal numbers, returns the correct mean", () => {
expect(calculateMean([-10, 20, -4])).toBe(2);
expect(calculateMean([1.5, 2.5, 3.5])).toBeCloseTo(2.5);
});

test("given an empty array, throws Error('calculateMean requires a non-empty array')", () => {
expect(() => calculateMean([])).toThrow(
new Error("calculateMean requires a non-empty array")
);
});

test("given a value that isn't an array, throws Error('calculateMean requires an array of numbers')", () => {
expect(() => calculateMean("banana")).toThrow(
new Error("calculateMean requires an array of numbers")
);
expect(() => calculateMean(42)).toThrow(
new Error("calculateMean requires an array of numbers")
);
expect(() => calculateMean(null)).toThrow(
new Error("calculateMean requires an array of numbers")
);
expect(() => calculateMean({})).toThrow(
new Error("calculateMean requires an array of numbers")
);
});

test("given no argument at all, throws Error('calculateMean requires an array of numbers')", () => {
expect(() => calculateMean()).toThrow(
new Error("calculateMean requires an array of numbers")
);
});

test("given an array containing a non-number value, throws Error('calculateMean requires an array of numbers')", () => {
expect(() => calculateMean([1, "2", 3])).toThrow(
new Error("calculateMean requires an array of numbers")
);
});
});
Loading
Loading