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
27 changes: 24 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@
// 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)) {
throw new Error("calculateMedian requires an array of numbers");
}

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

for (const item of list) {
if (!Number.isFinite(item)) {
throw new Error("calculateMedian requires an array of numbers");
}
}

const listLength = list.length;
const copy = [...list];
const middleIndex = Math.floor(listLength / 2);
copy.sort((a, b) => a - b);
Comment on lines +24 to +26

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 use Array.prototype.toSorted().

if (listLength % 2 === 0) {
const median = (copy[middleIndex - 1] + copy[middleIndex]) / 2;
return median;
} else {
return copy[middleIndex];
}
}

module.exports = calculateMedian;
15 changes: 14 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
function dedupe() {}
function dedupe(list) {
if (list.length === 0) {
return [];
}
const newList = [];
for (const item of list) {
if (newList.includes(item)) {
continue;
}
Comment on lines +7 to +9

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.

Note: Code on lines 7-11 could also be expressed without using else.

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.

Thanks for the suggestion! I've simplified the code by removing the unnecessary else.

newList.push(item);
}
return newList;
}
module.exports = dedupe;
48 changes: 39 additions & 9 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Dedupe Array

📖 Dedupe means **deduplicate**

In this kata, you will need to deduplicate the elements of an array
In this Kata, you will need to deduplicate the elements of an array

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]
Expand All @@ -16,13 +16,43 @@ 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");
describe("dedupe", () => {
it("given an empty array, it returns 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
it("given an array, it returns an deduplicate array", () => {
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]);
});

// 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 array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
it("Given an array with no duplicates, it returns a copy of the original array", () => {
const originalArray = ["d", "s", "t", "c"];
const result = dedupe(originalArray);

expect(result).toEqual(originalArray);
expect(result).not.toBe(originalArray);

expect(dedupe([5, 4, 3, 2, 1])).toEqual([5, 4, 3, 2, 1]);
expect(dedupe([7, 8, 9])).toEqual([7, 8, 9]);
});
Comment on lines +30 to +42

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.

Your function implementation is correct. However, this test could be improved to better ensure
that any future changes continue to align with the expected behavior described on line 32:

Then it should return a copy of the original array

This test should fail if the function returns the original array (instead of a copy of the original array).

The current tests check only if both the original array and the returned array contain identical elements.
In order to validate the returned array is a different array, we need an additional check.

Could you find out what this additional check is, and then implement it for one of the test cases?

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.

Thanks for the suggestion! I've added an additional check using not.toBe() to ensure the function returns a new array rather than the original one.


// 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("Given an array with duplicates, it removes duplicates while preserving the first occurrence", () => {
expect(dedupe(["d", "s", "s", "t", "c", "d", "s", "t", "c", "d"])).toEqual([
"d",
"s",
"t",
"c",
]);
expect(dedupe([5, 4, 5, 4, 2, 1, 5, 3, 4])).toEqual([5, 4, 2, 1, 3]);
expect(dedupe([7, 2, 7, 8, 9])).toEqual([7, 2, 8, 9]);
});
});
9 changes: 8 additions & 1 deletion Sprint-1/implement/describe-median.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ function calculateMedian(list) {
}

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

module.exports = describeMedian;
33 changes: 28 additions & 5 deletions Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,34 @@ 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"
describe("describeMedian", () => {
it("Given array of positive numbers, it returns the correct message", () => {
expect(describeMedian([1, 2, 6])).toEqual("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"
it("returns an error message when given an empty array", () => {
expect(describeMedian([])).toEqual(
"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 an error message when given an empty array", () => {
expect(describeMedian("banana")).toEqual(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
expect(describeMedian([3, "5", 8])).toEqual(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
expect(describeMedian(42)).toEqual(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
});
});

// Given something that isn't an array of numbers, e.g. "banana"
// When passed to describeMedian
Expand Down
22 changes: 21 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
function findMax(elements) {
}
if (!Array.isArray(elements)) {
throw new Error("findMax requires an array of numbers");
}
for (const value of elements) {
if (!Number.isFinite(value)) {
throw new Error("findMax requires an array of numbers");
}
}

const arrLength = elements.length;
let maxNum = -Infinity;
if (arrLength === 0) {
return [-Infinity];
}

for (const item of elements) {
if (maxNum < item) {
maxNum = item;
}
}
Comment on lines +17 to +21

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.

Note: This for loop alone could handle an array of any size if you initialise maxNum to the smallest possible value.

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.

Thanks for the suggestion! I've updated the initial value to -Infinity.

return maxNum;
}
module.exports = findMax;
97 changes: 78 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?

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.

Arrays containing NaN should throw an error. I've handled this using Number.isFinite().

Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,87 @@ 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("given an empty array,returns -Infinity", () => {
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("given an array with one number,returns that number", () => {
expect(findMax([5])).toEqual(5);
expect(findMax([20])).toEqual(20);
});

// 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
it("given an array with both positive and negative numbers, return the largest number overall", () => {
expect(findMax([5, 8, 3, 0, -1, 2, 16, -8, -16])).toEqual(16);
expect(findMax([-10, -5, -2, -20, 3, -1])).toEqual(3);
expect(findMax([-100, 50, -30, 25, 0, 75, -5])).toEqual(75);
});

// 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 just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
it("given an array with just negative numbers, return the closest one to zero", () => {
expect(findMax([-5, -8, -3, -76, -2, -16, -4])).toEqual(-2);
expect(findMax([-1, -2, -9, -100])).toEqual(-1);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
it("given an array with decimal numbers, returns the largest decimal number", () => {
expect(findMax([1.5, 3.7, 2.4, 5.9, 4.2])).toEqual(5.9);
expect(findMax([0.1, 0.9, 0.5])).toEqual(0.9);
expect(findMax([-1.5, -3.2, -0.5])).toEqual(-0.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")
// 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("throws when given an array containing a value that isn't a number", () => {
expect(() => findMax(["banana"])).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")
expect(() => findMax([10, "hello", 20])).toThrow(
new Error("findMax requires an array of numbers")
);

expect(() => findMax([10, null, 20])).toThrow(
new Error("findMax requires an array of numbers")
);

expect(() => findMax([10, NaN, 20])).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("throws when given something that isn't 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")
);

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

expect(() => findMax({})).toThrow(
new Error("findMax requires an array of numbers")
);
});
});
26 changes: 25 additions & 1 deletion Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
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");
}

for (const element of list) {
if (!Number.isFinite(element)) {
throw new Error("calculateMean requires an array of numbers");
}
}

let sum = 0;

list.forEach((item) => {
sum += item;
});

const mean = sum / list.length;

return mean;
}

module.exports = calculateMean;
Loading
Loading