Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
42e6670
Fix calculateMedian function, Implement error checking
MahirShah300 Oct 28, 2026
b20550f
Implement tests
MahirShah300 Oct 29, 2026
6af26a2
Implement findMax function
MahirShah300 Oct 29, 2026
7a3e4e5
Add tests
MahirShah300 Oct 29, 2026
36a519d
Fix error tests
MahirShah300 Oct 29, 2026
e97ee71
Fix error tests
MahirShah300 Oct 29, 2026
57f2875
Implement sum function
MahirShah300 Oct 29, 2026
1b7b599
Add tests
MahirShah300 Oct 29, 2026
479a30c
Implement dedupe function
MahirShah300 Oct 29, 2026
42bdd8d
Add tests
MahirShah300 Oct 30, 2026
03f21b0
Implement calculateMean function
MahirShah300 Oct 30, 2026
4d19b02
Add tests
MahirShah300 Oct 30, 2026
c3a4ffa
Implement describeMedian function
MahirShah300 Oct 30, 2026
8133c48
Refactor includes function to use for...of loop
MahirShah300 Oct 30, 2026
01acd63
Change name of currentValue to value. Add check to see if number is f…
MahirShah300 Nov 6, 2026
d166d5f
Made value in for loop const
MahirShah300 Nov 6, 2026
3ff7bca
Add check that the returned array is a copy and not the same array
MahirShah300 Nov 6, 2026
ca33de5
Simplify try catch to use thrown error message
MahirShah300 Nov 6, 2026
d4abcdf
Use more efficient method to find max element
MahirShah300 Nov 6, 2026
45748c8
Add check for infinite values. Use exported sum function from sum.js
MahirShah300 Nov 6, 2026
2d78b20
Correct decimal sum test to use toBeCloseTo
MahirShah300 Nov 6, 2026
60a8cf4
Declared loop variable const
MahirShah300 Nov 6, 2026
709eeb8
Change currentValue to value. Add check for infinite
MahirShah300 Nov 7, 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
26 changes: 24 additions & 2 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +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];
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");
}

if (
!list.every(
(value) =>
typeof value === "number" && !Number.isNaN(value) && Number.isFinite(value)

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 you find out which of these checks are redundant?

)
Comment thread
cjyuan marked this conversation as resolved.
Resolved
) {
throw new Error("calculateMedian requires an array of numbers");
}

const sortedList = list.toSorted((a, b) => a - b);
const middleIndex = Math.floor(sortedList.length / 2);
let median;
if (sortedList.length % 2 === 0) {
median = (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
} else {
median = sortedList[middleIndex];
}
return median;
}

Expand Down
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}
function dedupe(elements) {
const dedupedArray = [];

for (const value of elements) {
if (!dedupedArray.includes(value)) {
dedupedArray.push(value);
}
}
return dedupedArray;
}

module.exports = dedupe;
29 changes: 18 additions & 11 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ 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");
test("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
test("given an array with no duplicates, should return a copy of the same array", () => {
const testArray = [1, 5, 9, 23];
const testResult = dedupe(testArray);
expect(testResult).not.toBe(testArray);
expect(testResult).toEqual(testArray);
});

// 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 with strings or numbers, should return an array with duplicates removed and first occurrence preserved", () => {
expect(dedupe([1, 6, 1, "a", 5, 6, "a", "b", "b"])).toEqual([
1,
6,
"a",
5,
"b",
]);
});
8 changes: 7 additions & 1 deletion Sprint-1/implement/describe-median.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ function calculateMedian(list) {
}

// Implement this function. See describe-median.test.js for the acceptance criteria.
function describeMedian(list) {}
function describeMedian(list) {
try {
return "The median is " + calculateMedian(list);
} catch ({ name, message }) {
return "Could not calculate a median: " + message;
}
}

module.exports = describeMedian;
28 changes: 15 additions & 13 deletions Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ 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"
test('given [1, 2, 3], returns "The median is 2"', () => {
expect(describeMedian([1, 2, 3])).toEqual("The median is 2");
});

test('given empty array, returns "Could not calculate a median: calculateMedian requires a non-empty array"', () => {
expect(describeMedian([])).toEqual(
"Could not calculate a median: calculateMedian requires a non-empty array"
);
});

test('given something that isnt an array of numbers, returns "Could not calculate a median: calculateMedian requires an array of numbers"', () => {
expect(describeMedian("banana")).toEqual(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
});
18 changes: 18 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
function findMax(elements) {
if (!Array.isArray(elements)) {
throw new Error("findMax requires an array of numbers");
}

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

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

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

module.exports = findMax;
60 changes: 31 additions & 29 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,34 @@ We have set things up already so that this file can see your function from the o

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")
test("given an empty array, returns -Infinity", () => {
expect(findMax([])).toEqual(-Infinity);
});

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

test("given an array with positive and negative numbers, returns largest number overall", () => {
expect(findMax([10, 58, -74, 984, -87, -995, 47])).toEqual(984);
});

test("given an array with only negative numbers, returns number closest to zero", () => {
expect(findMax([-58, -65, -7, -2, -96, -753])).toEqual(-2);
});

test("given an array with decimal numbers, returns largest decimal number", () => {
expect(findMax([10, 9.5, 65.6, 41, 36.8])).toEqual(65.6);
});

test("given an array with with value that isn't a number, throws error", () => {
expect(() => findMax([10, 23, 18, "hello", 89, 2])).toThrow(
new Error("findMax requires an array of numbers")
);
});

test("given something that isn't an array, throws error", () => {
expect(() => findMax("Hello")).toThrow(
new Error("findMax requires an array of numbers")
);
});
28 changes: 27 additions & 1 deletion Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
function calculateMean(list) {}
const sum = require("./sum.js");

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.every(
(value) =>
typeof value === "number" &&
!Number.isNaN(value) &&
Number.isFinite(value)
)
Comment thread
cjyuan marked this conversation as resolved.
Resolved
) {
throw new Error("calculateMean requires an array of numbers");
}

const total = sum(list);
const mean = total / list.length;

return mean;
}

module.exports = calculateMean;
64 changes: 35 additions & 29 deletions Sprint-1/implement/mean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,38 @@ 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")
test("given [1, 2, 6], returns 3", () => {
expect(calculateMean([1, 2, 6])).toEqual(3);
});

test("given array with single number, return that number", () => {
expect(calculateMean([5])).toEqual(5);
});

test("given an array with positive and negative numbers, return the correct mean", () => {
expect(calculateMean([5, 8, -2, 7, -9])).toBeCloseTo(1.8);
});

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

test("given something that isn't an array, throw an error", () => {
expect(() => calculateMean("banana")).toThrow(
"calculateMean requires an array of numbers"
);
});

test("given no argument, throw an error", () => {
expect(() => calculateMean()).toThrow(
"calculateMean requires an array of numbers"
);
});

test("given an array with non-number values, throw an error", () => {
expect(() => calculateMean([1, 5, 9, "hello", 4])).toThrow(
"calculateMean requires an array of numbers"
);
});
20 changes: 20 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
function sum(elements) {
if (!Array.isArray(elements)) {
throw new Error("sum requires an array of numbers");
}

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

let total = 0;
for (number of elements) {
total += number;
}
return total;
}

module.exports = sum;
51 changes: 27 additions & 24 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,30 @@ const sum = require("./sum.js");

// Acceptance Criteria:

// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")

// Given an array with just one number
// When passed to the sum function
// Then it should return that number

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

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

// 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")
test("given an empty array, returns 0", () => {
expect(sum([])).toEqual(0);
});

test("given an array with one number, return that number", () => {
expect(sum([5])).toEqual(5);
});

test("given an array with negative numbers, correctly return sum of numbers", () => {
expect(sum([-10, -45, -6, -74])).toEqual(-135);
});

test("given an array with decimal numbers, correctly return total sum", () => {
expect(sum([12.7, 3.6, 32.48, 8, 32.53])).toBeCloseTo(89.31);
});
Comment thread
cjyuan marked this conversation as resolved.
Resolved

test("given an array with some values that aren't numbers, throw an Error", () => {
expect(() => {
sum([10, 8, 32, "hello", 2, 53, 32]);
}).toThrow(new Error("sum requires an array of numbers"));
});

test("given something that isn't an array, throw an Error", () => {
expect(() => {
sum("hello");
}).toThrow(new Error("sum requires an array of numbers"));
});
Loading
Loading