Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
bd388e4
Implement input validation and error handling in calculateMedian func…
rahanasuleiman8-ship-it Nov 1, 2026
dbf4e21
Add a blank line for better readability in dedupe test file
rahanasuleiman8-ship-it Nov 1, 2026
1e13f78
Add tests for dedupe function to validate behavior with empty and uni…
rahanasuleiman8-ship-it Nov 1, 2026
d940b04
Implement dedupe function to remove duplicate values from an array
rahanasuleiman8-ship-it Nov 1, 2026
26c125e
Implement describeMedian function to calculate and return the median …
rahanasuleiman8-ship-it Nov 1, 2026
96de195
Add tests for describeMedian function to validate median calculation …
rahanasuleiman8-ship-it Nov 1, 2026
9ec2074
Add test case for describeMedian to handle empty array input
rahanasuleiman8-ship-it Nov 1, 2026
547e68b
Add test case for describeMedian to handle non-array input
rahanasuleiman8-ship-it Nov 1, 2026
fe34b63
Refactor describeMedian tests to remove todos and ensure correct erro…
rahanasuleiman8-ship-it Nov 1, 2026
411ad3f
Refactor describeMedian tests to improve clarity and organization
rahanasuleiman8-ship-it Nov 1, 2026
1c76aa4
Improve error handling in describeMedian by capturing error parameter
rahanasuleiman8-ship-it Nov 1, 2026
34fbfe5
Implement findMax function with input validation and error handling
rahanasuleiman8-ship-it Nov 1, 2026
866d1d0
Add tests for findMax function to cover various scenarios including e…
rahanasuleiman8-ship-it Nov 1, 2026
bc3b0d2
Implement calculateMean function with input validation and error hand…
rahanasuleiman8-ship-it Nov 2, 2026
ddaa794
Add test for calculateMean function to validate mean calculation for …
rahanasuleiman8-ship-it Nov 2, 2026
3e45546
Add test for calculateMean function to verify single number input
rahanasuleiman8-ship-it Nov 2, 2026
b1069fe
Add test for calculateMean function to validate mean calculation for …
rahanasuleiman8-ship-it Nov 2, 2026
4faa807
Add tests for calculateMean function to verify error handling for emp…
rahanasuleiman8-ship-it Nov 2, 2026
35df2b2
Add test for calculateMean function to verify error handling for no a…
rahanasuleiman8-ship-it Nov 2, 2026
9d10901
Add tests for calculateMean function to validate error handling for n…
rahanasuleiman8-ship-it Nov 2, 2026
292c6ed
Update test for calculateMean function to use different input values …
rahanasuleiman8-ship-it Nov 2, 2026
59d9646
Add sum function to calculate the total of an array, including input …
rahanasuleiman8-ship-it Nov 2, 2026
55d7528
Add tests for sum function to validate various input scenarios and er…
rahanasuleiman8-ship-it Nov 2, 2026
02e7fdc
Refactor includes function to use for...of loop for improved readability
rahanasuleiman8-ship-it Nov 2, 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
34 changes: 30 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,39 @@
// 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



// 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) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
// 1. Check if 'list' is a valid array and not null/undefined
if (!Array.isArray(list)) {
throw new Error("calculateMedian requires an array of numbers");
}

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

// 3. Check if every item in the array is a valid number (no strings, null, undefined, etc.)
for (const item of list) {
if (typeof item !== "number" || Number.isNaN(item)) {
throw new Error("calculateMedian requires an array of numbers");
}
}

// 4. Create a sorted copy of the array so we don't mutate the original input
const sorted = [...list].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);

// 5. Calculate median for odd vs even length arrays
if (sorted.length % 2 !== 0) {
return sorted[mid];
} else {
return (sorted[mid - 1] + sorted[mid]) / 2;
}
}

module.exports = calculateMedian;
module.exports = calculateMedian;
5 changes: 4 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
function dedupe() {}
function dedupe(arr) {
return [... new Set(arr)]
}
module.exports = dedupe;
22 changes: 22 additions & 0 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,33 @@ E.g. dedupe([1, 2, 1]) returns [1, 2]
// Then it should return an empty array
test.todo("given an empty array, it returns an empty array");

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
test.todo("given an array, it returns a copy of the original array");

it("given an array, 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);
});

// 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.todo("given an array, it returns a duplicate while preserving the first occurrence of each element from the original array");

it("given an array, it removes duplicates while preserving the first occurrence of elements", () => {
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]);
});
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;
31 changes: 24 additions & 7 deletions Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,27 @@ const describeMedian = require("./describe-median.js");
// 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("given [1, 2, 3], returns {The median is 2}", () => {
expect(describeMedian([1, 2, 3])).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, returns the correct error message", () => {
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 an array, returns the correct error message", () => {
expect(describeMedian("banana")).toEqual(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
});
});
19 changes: 19 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
function findMax(elements) {
// 1. Check if input is a valid array and not null/undefined
if (!Array.isArray(elements)) {
throw new Error("findMax requires an array of numbers");
}

// 2. Handle empty array edge case per requirements
if (elements.length === 0) {
return -Infinity;
}

// 3. Validate that every item in the array is strictly a number (no strings, null, etc.)
for (const item of elements) {
if (typeof item !== "number" || Number.isNaN(item)) {
throw new Error("findMax requires an array of numbers");
}
}

// 4. Return the maximum number using Math.max
return Math.max(...elements);
}

module.exports = findMax;
71 changes: 52 additions & 19 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,61 @@ 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

// 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 one number, returns that number", () => {
expect(findMax([42])).toEqual(42);
});

// 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 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, returns the largest number overall", () => {
expect(findMax([30, -50, 10, 40, -5])).toEqual(40);
});

// 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
it("given an array with just negative numbers, returns the closest one to zero", () => {
expect(findMax([-10, -3, -50, -5])).toEqual(-3);
});

// 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 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, 2.7, 2.1, 0.4])).toEqual(2.7);
});

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

// 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 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")
);
});
});
26 changes: 24 additions & 2 deletions Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
function calculateMean(list) {}
function calculateMean(list) {
// 1. Check if input is a valid array
if (!Array.isArray(list)) {
throw new Error("calculateMean requires an array of numbers");
}

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

// 3. Validate that every item in the array is strictly a number
let sum = 0;
for (const item of list) {
if (typeof item !== "number" || Number.isNaN(item)) {
throw new Error("calculateMean requires an array of numbers");
}
sum += item;
}

// 4. Return the calculated mean
return sum / list.length;
}

module.exports = calculateMean;
91 changes: 69 additions & 22 deletions Sprint-1/implement/mean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,79 @@ 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");
describe("calculateMean", () => {
// Given an array of numbers
// When passed to calculateMean
// Then it should return their mean

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

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

// Given an empty array
// When passed to calculateMean
// Then it should throw Error("calculateMean requires a non-empty array")
it("given an array with a single number, returns that number", () => {
expect(calculateMean([10])).toEqual(10);
});

// 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 an array containing negative or decimal numbers
// When passed to calculateMean
// Then it should return the correct mean

// Given no argument at all
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")
it("given an array containing negative or decimal numbers, returns the correct mean", () => {
expect(calculateMean([6, -2, 8])).toEqual(4); // 6 + (-2) + 8 = 12 / 3 = 4
expect(calculateMean([1.5, 2.5])).toEqual(2);
});

// 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")
// Given an empty array
// When passed to calculateMean
// Then it should throw Error("calculateMean requires a non-empty array")

it("given an empty array, throws the correct error", () => {
expect(() => calculateMean([])).toThrow(
new 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")

it("given a value that isn't an array (string, number, null, object), throws the correct error", () => {
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")
);
});

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

it("given no argument at all, throws the correct error", () => {
expect(() => calculateMean()).toThrow(
new 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")

it("given an array containing a non-number value, throws the correct error", () => {
expect(() => calculateMean([1, "2", 3])).toThrow(
new Error("calculateMean requires an array of numbers")
);
});
});
23 changes: 22 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
function sum(elements) {
// 1. Check if input is a valid array
if (!Array.isArray(elements)) {
throw new Error("sum requires an array of numbers");
}

// 2. Handle empty array edge case (sum of empty array is 0)
if (elements.length === 0) {
return 0;
}

// 3. Validate that every item in the array is strictly a number
let total = 0;
for (const item of elements) {
if (typeof item !== "number" || Number.isNaN(item)) {
throw new Error("sum requires an array of numbers");
}
total += item;
}

// 4. Return the calculated sum
return total;
}

module.exports = sum;
module.exports = sum;
Loading
Loading