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: 23 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,29 @@
// or contains values that aren't numbers (the function is expected to throw - see the tests).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
if (Array.isArray(list) === false) {
throw new Error("calculateMedian requires an array of numbers");
}

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

if (list.some((item) => typeof item === "string" || item === null || item === undefined)) {
throw new Error("calculateMedian requires an array of numbers");
}
Comment on lines 17 to 19

@cjyuan cjyuan Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could also just check whether item is NOT a value of type number, or even better, check if the item is NOT a finite number.

Even though the test script does not include any test cases involving all possible non-number values ,the comment in the test script does mention:

Arrays containing any non-number value should throw


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

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

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(list) {
return [...new Set(list)];
}

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

📖 Dedupe means **deduplicate**
//Dedupe Array

In this kata, you will need to deduplicate the elements of an array
//📖 Dedupe means **deduplicate**

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]
*/
//In this kata, you will need to deduplicate the elements of an array

// Acceptance Criteria:
describe("dedupe", () => {
//E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c']
it("returns for ['a','a','a','b','b','c']", () => {
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
});

// 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");
//E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8]
it("returns for [5, 1, 1, 2, 3, 2, 5, 8]", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});
//E.g. dedupe([1, 2, 1]) returns [1, 2]
//*/
it("returns for [1, 2, 1]", () => {
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
// Acceptance Criteria:

// 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.
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array

it("returns for []", () => {
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

it("returns for [1, 2, 3, 4]", () => {
const original = [1, 2, 3, 4];
const result = dedupe(original);
expect(result).not.toBe(original);
});
Comment thread
cjyuan marked this conversation as resolved.
Resolved
Comment on lines +39 to +43

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In addition to checking whether result is a copy of original, this test should also check if result and original contains identical elements.


// 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.
it("returns for ['apple', 'orange', 'banana', 'banana']", () => {
expect(dedupe(["apple", "orange", "banana", "banana"])).toEqual([
"apple",
"orange",
"banana",
]);
});
});
11 changes: 7 additions & 4 deletions Sprint-1/implement/describe-median.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Don't change this function.
function calculateMedian(list) {
if (!Array.isArray(list)) {
throw new Error("calculateMedian requires an array of numbers");
throw new Error("Could not calculate a median: calculateMedian requires an array of numbers");
}
if (list.length === 0) {
throw new Error("calculateMedian requires a non-empty array");
throw new Error("Could not calculate a median: calculateMedian requires a non-empty array");
}
for (const item of list) {
if (typeof item !== "number") {
throw new Error("calculateMedian requires an array of numbers");
throw new Error("Could not calculate a median: calculateMedian requires an array of numbers");
}
}
const middleIndex = Math.floor(list.length / 2);
Expand All @@ -20,6 +20,9 @@ function calculateMedian(list) {
}

// Implement this function. See describe-median.test.js for the acceptance criteria.
function describeMedian(list) {}
function describeMedian(list) {
const result = calculateMedian(list);
return `The median is ${result}`
}

module.exports = describeMedian;
38 changes: 29 additions & 9 deletions Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,32 @@ const describeMedian = require("./describe-median.js");
// 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", () => {
it("returns for [1, 2, 3, 4]", () => {
const array = [1, 2, 3, 4];
const result = describeMedian(array);
expect(result).toBe("The median is 2.5");
});

// Given an empty array
// When passed to describeMedian
// Then it should return "Could not calculate a median: calculateMedian requires a non-empty array"
it("returns for []", () => {
expect(() => describeMedian([])).toThrow(
new Error(
"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"
it("returns for ['banana', 'apple', 'orange']", () => {
expect(() => describeMedian(["banana", "apple", "orange"])).toThrow(
new Error(
"Could not calculate a median: calculateMedian requires an array of numbers"
)
);
});
});
8 changes: 7 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function findMax(elements) {
if (!Array.isArray(elements)) {
throw new Error("findMax requires an array of numbers");
}
if (elements.some((element) => typeof element === "string")) {
throw new Error("findMax requires an array of numbers");
}
return Math.max(...elements);
}

module.exports = findMax;
82 changes: 63 additions & 19 deletions Sprint-1/implement/max.test.js

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • How should the function handle an array containing NaN?

  • How should the function handle an array containing elements that are not numbers or strings (e.g., null, undefined, true, console (object), etc.)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The assessment seems do not require to handle NaN and array which are neither numbers nor strings.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • NaN is also a value of type "number". Handling NaN is optional in this exercise but you should find out how to check if a value is not NaN.

  • The comment in max.test.js does mention "any value that isn't a 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")
    

    So the function should also be able to throw an error when the given array contains any value that is not a number.

Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,72 @@ const findMax = require("./max.js");
// 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");
describe("findMax", () => {
it("returns for []", () => {
expect(findMax([])).toEqual(-Infinity);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
// Given an array with one number
// When passed to the max function
// Then it should return that number
it("returns for [17]", () => {
expect(findMax([17])).toEqual(17);
});

// 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 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
it("returns for [-6, 15, 30, 42, -90]", () => {
expect(findMax([-6, 15, 30, 42, -90])).toEqual(42);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
// 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 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")
it("returns for [-6, 15, 30, 42, -90]", () => {
expect(findMax([-6, 15, 30, 42, -90])).toEqual(42);
});

// 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")
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

it("returns for [0.5, 1.5, 3.5, 5.5, 7.5]", () => {
expect(findMax([0.5, 1.5, 3.5, 5.5, 7.5])).toEqual(7.5);
});

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

it("returns for [2, 4, 'hello', 6, 8, 10]", () => {
() =>
expect(findMax([2, 4, "hello", 6, 8, 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
// Then it should throw Error("findMax requires an array of numbers")

it("returns for 'hey'", () => {
expect(() => findMax("hey")).toThrow(
new Error("findMax requires an array of numbers")
);
});

it("returns for 42", () => {
expect(() => findMax(42)).toThrow(
new Error("findMax requires an array of numbers")
);
});

it("returns for ''", () => {
expect(() => findMax()).toThrow(
new Error("findMax requires an array of numbers")
);
});
});
23 changes: 22 additions & 1 deletion Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
function calculateMean(list) {}
function calculateMean(list) {
if (!Array.isArray(list)) {
throw new Error("calculateMean requires an array of numbers");
}

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

if (list.some((item) => typeof item === "string")) {
throw new Error("calculateMean requires an array of numbers");
}

let sum = 0;
for (const item of list) {
sum += item;
}

const mean = sum / list.length;

return mean;
}

module.exports = calculateMean;
Loading
Loading