Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d1362cd
Refactor calculateMedian so even length lists give correct median
habohlin Oct 30, 2026
ca7d11e
Sorted the list in a non mutating way before finding middle value
habohlin Oct 30, 2026
c5a3e4e
Add sorting function to toSorted so it sorts ascending
habohlin Oct 31, 2026
882c3a9
Removing slice to not mutate array and refactor return statement
habohlin Oct 31, 2026
5fa46ec
Now throws when list is empty array
habohlin Oct 31, 2026
88ea97f
Now throws when list is a single string
habohlin Oct 31, 2026
474445f
Throws when list is a number
habohlin Oct 31, 2026
73722de
Throws when list is null
habohlin Oct 31, 2026
5d20017
Throws when list is object
habohlin Oct 31, 2026
9843b6b
Set parameter to default value making it throw if attribute isn't spe…
habohlin Oct 31, 2026
d2a28a9
Throws when list contains any strings
habohlin Oct 31, 2026
07073aa
Throws when list contains null
habohlin Oct 31, 2026
fe7960b
Throws when list containes undefined value
habohlin Oct 31, 2026
995d91e
Returns -infinity for empty array passed
habohlin Nov 3, 2026
74383ea
Returns solo number passed in array
habohlin Nov 3, 2026
0a5abe5
Returns the biggest value in an array regardless of negative or decim…
habohlin Nov 3, 2026
9568c85
Throws when non number part of the array
habohlin Nov 3, 2026
bae2a3b
Throws when passes non array
habohlin Nov 3, 2026
1147d90
Returns 0 when passed empty array
habohlin Nov 3, 2026
c090717
Returns the number when passed array with single number
habohlin Nov 3, 2026
e7d3cbb
Returns correct sum regardless of negative and decimal values present
habohlin Nov 3, 2026
98b9810
Throws when given array containing non number
habohlin Nov 3, 2026
76b199a
Throws when passed argument that isn't array
habohlin Nov 3, 2026
4408d15
When passed empty array, returns empty array
habohlin Nov 3, 2026
f76781f
Returns same array when no duplicates present
habohlin Nov 3, 2026
f1278f1
Dedupe now returns deduped array
habohlin Nov 3, 2026
cce090b
Function returns mean of array of numbers, negative, decimal, solo nu…
habohlin Nov 3, 2026
0accc03
Throws when passed empty array
habohlin Nov 3, 2026
dec5bce
Throws when passed non-array
habohlin Nov 3, 2026
6caf465
Already throws for passing no value
habohlin Nov 3, 2026
aaf6237
Throws when sent an array including a non-number
habohlin Nov 3, 2026
4df5d9b
Returns the correct message when given an array of numbers
habohlin Nov 3, 2026
795191f
Catches error and displays error message when passed empty array
habohlin Nov 3, 2026
0b34659
Already catches thrown error for array containing non-number or non n…
habohlin Nov 3, 2026
1452e8f
Refactor includes funtion to use for of loop
habohlin Nov 3, 2026
2adeba3
Format all files with prettier
habohlin Nov 3, 2026
a94cc17
Format all files for real with prettier
habohlin Nov 3, 2026
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
38 changes: 35 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,42 @@
// 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).

function calculateMedian(list) {
function calculateMedian(list = 0) {
if (list === null) {
throw new Error("calculateMedian requires an array of numbers");
}

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

if (typeof list === "string") {
throw new Error("calculateMedian requires an array of numbers");
}

if (typeof list === "number") {
throw new Error("calculateMedian requires an array of numbers");
}

if (typeof list === "object" && !Array.isArray(list)) {
throw new Error("calculateMedian requires an array of numbers");
}
Comment on lines +17 to +27

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In the future, you may consider combining these three conditions into one by checking if (!Array.isArray(list)).


for (item of list) {
if (typeof item === "string" || item === null || item === undefined) {
throw new Error("calculateMedian requires an array of numbers");
}
}

const sortedList = list.toSorted((a, b) => a - b);

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

if (list.length % 2 === 0) {
return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
}

return sortedList[middleIndex];
}

module.exports = calculateMedian;
14 changes: 13 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
function dedupe() {}
function dedupe(elements) {
const dedupedArr = [];

for (element of elements) {
if (!dedupedArr.includes(element)) {
dedupedArr.push(element);
}
}

return dedupedArr;
}

module.exports = dedupe;
12 changes: 10 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ 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");
it("given an empty array, returns 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 array without duplicates, returns that array", () => {
expect(dedupe([4, 6, 9, 23])).toEqual([4, 6, 9, 23]);
});

// 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
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
it("given array with duplicates, returns array without duplicates", () => {
expect(dedupe([4, 6, 9, 23, 9, 23, 23])).toEqual([4, 6, 9, 23]);
});
Comment on lines +34 to +36

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Only numbers has been tested here. How about array of strings, or array of mixed numbers and strings? Please add those tests as well. Thank you.

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 median = calculateMedian(list);
return "The median is " + median;
} catch (error) {
return "Could not calculate a median: " + error.message;
}
}

module.exports = describeMedian;
20 changes: 19 additions & 1 deletion Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,30 @@ 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"');
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("Given an empty array, it tells you it couldn't calculate the median and why", () => {
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("Given something that isn't array of numbers, it tells you it couldn't calculate the median and why", () => {
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"
);
Comment on lines +40 to +49

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Well done. Good test cases coverage.

});
20 changes: 20 additions & 0 deletions 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");
}

if (elements.length === 0) {
return -Infinity;
}

let max = elements[0];

for (element of elements) {
if (typeof element != "number") {
throw new Error("findMax requires an array of numbers");
}
if (element > max) {
max = element;
}
}

return max;
}

module.exports = findMax;
32 changes: 31 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,58 @@ 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");
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
it("Given array with one number should return that number", () => {
expect(findMax([3])).toEqual(3);
});

// 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 array of positive and negative values returns the biggest", () => {
expect(findMax([4, -7, 47, -90])).toEqual(47);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
it("Given array of negative values returns the closest to zero", () => {
expect(findMax([-4, -7, -47, -90])).toEqual(-4);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
it("Given array of positive and negative values returns the biggest", () => {
expect(findMax([4.3, -7.83, 47.134, -90.2947])).toEqual(47.134);
});

// 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("Given array with value that isn't a number, should throw", () => {
expect(() => {
findMax([5, "ooo", 203]);
}).toThrow("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("Given argument that isn't an array, should throw", () => {
expect(() => {
findMax("hey");
}).toThrow("findMax requires an array of numbers");
expect(() => {
findMax(42);
}).toThrow("findMax requires an array of numbers");
expect(() => {
findMax();
}).toThrow("findMax requires an array of numbers");
});
20 changes: 19 additions & 1 deletion Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
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");
}

let sum = 0;

for (item of list) {
if (typeof item != "number") {
throw new Error("calculateMean requires an array of numbers");
}
sum += item;
}

return sum / list.length;
}

module.exports = calculateMean;
39 changes: 38 additions & 1 deletion Sprint-1/implement/mean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,65 @@ const calculateMean = require("./mean.js");
// 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");
it("Given array of positive numbers, it returns their mean", () => {
expect(calculateMean([1, 2, 6])).toEqual(3);
});

// Given an array with a single number
// When passed to calculateMean
// Then it should return that number
it("Given array of one number, it returns that number", () => {
expect(calculateMean([2])).toEqual(2);
});

// Given an array containing negative or decimal numbers
// When passed to calculateMean
// Then it should return the correct mean
it("Given array of negative or decimal number, it returns their mean", () => {
expect(calculateMean([1, -2, 6, 5.89, -43.284])).toEqual(-6.4788);
});

// Given an empty array
// When passed to calculateMean
// Then it should throw Error("calculateMean requires a non-empty array")
it("Given an empty array, it should throw", () => {
expect(() => {
calculateMean([]);
}).toThrow("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")
it("Given value that isn't array, it should throw", () => {
expect(() => {
calculateMean("banana");
}).toThrow("calculateMean requires an array of numbers");
expect(() => {
calculateMean(42);
}).toThrow("calculateMean requires an array of numbers");
expect(() => {
calculateMean(null);
}).toThrow("calculateMean requires an array of numbers");
expect(() => {
calculateMean({});
}).toThrow("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")
it("Given no value at all, it should throw", () => {
expect(() => {
calculateMean();
}).toThrow("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")
it("Given array containing non-number, it should throw", () => {
expect(() => {
calculateMean([1, "2", 3]);
}).toThrow("calculateMean requires an array of numbers");
});
14 changes: 14 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
function sum(elements) {
if (!Array.isArray(elements)) {
throw new Error("sum requires an array of numbers");
}

let theSum = 0;

for (element of elements) {
if (typeof element != "number") {
throw new Error("sum requires an array of numbers");
}
theSum += element;
}

return theSum;
}

module.exports = sum;
29 changes: 28 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,51 @@ const sum = require("./sum.js");
// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")
it("given an empty array, returns 0", () => {
expect(sum([])).toEqual(0);
});

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

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
it("given array with negative numbers, returns the correct sum", () => {
expect(sum([6, -10, 3, 5])).toEqual(4);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
it("given array with decimal numbers, returns the correct sum", () => {
expect(sum([6, 3.68, 3, 5])).toEqual(17.68);
});

// Given an array containing a value that isn't a number
// When passed to the sum function
// Then it should throw Error("sum requires an array of numbers")
it("given array with values that aren't numbers, should throw", () => {
expect(() => {
sum([6, 3.68, 3, "hey", 5]);
}).toThrow("sum 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 sum function
// Then it should throw Error("sum requires an array of numbers")
it("Given argument that isn't an array, should throw", () => {
expect(() => {
sum("hey");
}).toThrow("sum requires an array of numbers");
expect(() => {
sum(42);
}).toThrow("sum requires an array of numbers");
expect(() => {
sum();
}).toThrow("sum requires an array of numbers");
});
5 changes: 2 additions & 3 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Refactor the implementation of includes to use a for...of loop

function includes(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
if (element === target) {
for (item of list) {
if (item === target) {
return true;
}
}
Expand Down
Loading