Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
09fe065
Fix median calculation to return correct value from the list
moses77-boop Oct 21, 2026
1fde0e0
Fix median calculation to handle both odd and even length lists
moses77-boop Oct 21, 2026
ecfa4aa
Fix median calculation to use sorted list
moses77-boop Oct 21, 2026
fd8ec66
Add error handling for input validation to calculateMedian function
moses77-boop Oct 21, 2026
e3e6ea5
Add error handling for empty array
moses77-boop Oct 22, 2026
e95631c
Add error handling to ensure all elements in the array are numbers
moses77-boop Oct 22, 2026
bba68b6
Implement test for empty array case in findMax function
moses77-boop Oct 22, 2026
66a3e6a
Add input validation to findMax function to ensure an array is provided
moses77-boop Oct 22, 2026
ed01080
Refactor findMax function to include max initialization
moses77-boop Oct 22, 2026
b564d44
Add test for single number array case in findMax function
moses77-boop Oct 22, 2026
f104afb
Refactor findMax function
moses77-boop Oct 22, 2026
b6f529d
Add test for non-number input in findMax function
moses77-boop Oct 22, 2026
c7b0e6e
Add test for non-array inputs in findMax function
moses77-boop Oct 22, 2026
872a29b
Add test for finding the maximum number in an array with positive and…
moses77-boop Oct 22, 2026
6db3742
Add test for finding the closest number to zero in an array of negati…
moses77-boop Oct 22, 2026
82f5c3a
Add test for finding the largest decimal number in an array
moses77-boop Oct 22, 2026
7aaff3c
Add test for handling non-number elements in sum function
moses77-boop Oct 22, 2026
02c2e87
Add error handling for non-array inputs in sum function
moses77-boop Oct 22, 2026
44bf4cf
Implement test for returning 0 when given an empty array in sum function
moses77-boop Oct 22, 2026
0a3d100
Refactor sum function to improve error handling and ensure input is a…
moses77-boop Oct 22, 2026
5506d62
Add test for handling non-array inputs in sum function
moses77-boop Oct 22, 2026
1507d42
Add test for handling single number input in sum function
moses77-boop Oct 22, 2026
cc8664c
Add test for handling negative numbers in sum function
moses77-boop Oct 22, 2026
c229437
Add test for handling decimal numbers in sum function
moses77-boop Oct 22, 2026
6b578bf
Fix test for decimal numbers in sum function to use toBeCloseTo for p…
moses77-boop Oct 22, 2026
4b21f36
Fix test for decimal numbers in sum function to return correct total
moses77-boop Oct 22, 2026
6fc8984
Implement export command for dedupe function
moses77-boop Oct 23, 2026
fb39a6b
Implement test for dedupe function to handle empty array case
moses77-boop Oct 23, 2026
4ee500d
Implement dedupe function
moses77-boop Oct 23, 2026
6d78cdb
Implement test for dedupe function to handle arrays with no duplicates
moses77-boop Oct 23, 2026
7130597
Refactor dedupe function to accept an array parameter and iterate thr…
moses77-boop Oct 23, 2026
b3bd915
Add test for dedupe function to handle arrays with duplicate strings
moses77-boop Oct 23, 2026
aafec32
Refactor dedupe function to prevent adding duplicates to the uniqueIt…
moses77-boop Oct 23, 2026
70f312c
Add test for dedupe function to handle arrays of numbers with duplicates
moses77-boop Oct 23, 2026
6f4c2d4
Implement test for calculateMean function to verify mean calculation …
moses77-boop Oct 23, 2026
f96c928
Implement calculateMean function to compute the average of an array o…
moses77-boop Oct 23, 2026
9b1afc7
Add tests for calculateMean function to handle single numbers, negati…
moses77-boop Oct 23, 2026
84122b3
Add test for calculateMean function to verify error handling for empt…
moses77-boop Oct 23, 2026
7eeab41
Add error handling to calculateMean function for empty arrays
moses77-boop Oct 23, 2026
bece9ad
Add tests for calculateMean function to handle non-array inputs
moses77-boop Oct 23, 2026
c0a278a
Add input validation to calculateMean function for non-array inputs
moses77-boop Oct 23, 2026
0bb1418
Add test for calculateMean function to handle no arguments
moses77-boop Oct 23, 2026
55521a9
Add test for calculateMean function to handle non-number values in array
moses77-boop Oct 23, 2026
cd600e2
Add input validation for array elements in calculateMean function
moses77-boop Oct 23, 2026
0ab70f6
Implement test for describeMedian function to validate median calcula…
moses77-boop Oct 23, 2026
3cd3611
Implement describeMedian function to return median value as a string
moses77-boop Oct 23, 2026
783326c
Add tests for empty array cases
moses77-boop Oct 23, 2026
b9f0e90
Add error handling to describeMedian function for invalid input
moses77-boop Oct 23, 2026
74b3e77
Fix test typo in error message for empty array case
moses77-boop Oct 23, 2026
c08e109
Add test for non-number value of array case in describeMedian and fix…
moses77-boop Oct 23, 2026
435bc33
Fix typo in error message for median calculation failure
moses77-boop Oct 23, 2026
068ad9d
Refactor includes function to use for...of loop for improved readability
moses77-boop Oct 23, 2026
3f5b5d3
Update input data for aoc-2018-day1 with new values and formatting ad…
moses77-boop Oct 23, 2026
d1b8c93
Implement frequency calculation logic and handle duplicate frequencies
moses77-boop Oct 23, 2026
de542e0
Refactor loop structure for improved readability in frequency calcula…
moses77-boop Oct 24, 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
25 changes: 23 additions & 2 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) {

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 (typeof item !== "number") {
throw new Error("calculateMedian requires an array of numbers");
}
}

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

const lowerMiddle = sortedList[middleIndex - 1];
const upperMiddle = sortedList[middleIndex];
return (lowerMiddle + upperMiddle) / 2;
}

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

for (let i = 0; i < originalArray.length; i++) {
const currentItem = originalArray[i];
const alreadyInArray = uniqueItems.includes(currentItem);

if (alreadyInArray === false) {
uniqueItems.push(currentItem);
}
}

return uniqueItems;
}

module.exports = dedupe;
19 changes: 18 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,30 @@ 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");
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, it returns a copy of the original array", () => {
const input = [1, 2, 3, 4, 5];
const result = dedupe(input);

expect(result).toEqual([1, 2, 3, 4, 5]);
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("given an array of strings with duplicates, it removes them and keeps the first occurrence", () => {
expect(dedupe(["a", "a", "b", "b", "b", "c"])).toEqual(["a", "b", "c"]);
});

test("given an array of numbers with duplicates, it removes them and keeps the first occurrence", () => {
expect(dedupe([1, 2, 2, 3, 3, 3, 4, 4, 5, 5])).toEqual([1, 2, 3, 4, 5]);
expect(dedupe([5, 5, 2, 1, 1,])).toEqual([5, 2, 1]);
});
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;
10 changes: 9 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,20 @@ 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"');
test('given [1, 2, 3], returns "The median is 2"', () => {
expect(describeMedian([1, 2, 3])).toBe("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"
test('given an empty array, returns "Could not calculate a median: calculateMedian requires a non-empty array"', () => {
expect(describeMedian([])).toBe("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 something that is not an array, returns "Could not calculate a median: calculateMedian requires an array of numbers"', () => {
expect(describeMedian("banana")).toBe("Could not calculate a median: calculateMedian requires an array of numbers");
});
16 changes: 16 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
function findMax(elements) {
if(!Array.isArray(elements)) {
throw new Error("findMax requires an array of numbers");
}

let max = -Infinity;

for (const element of elements) {
if (typeof element !== "number" || Number.isNaN(element)) {
throw new Error("findMax requires an array of numbers");
}

if (element > max) {
max = element;
}
}
return max;
}

module.exports = findMax;
24 changes: 23 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,50 @@ 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");
test("given an empty array, returns -Infinity", ()=> {
expect(findMax([])).toBe(-Infinity);
});

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

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test("given positive and negative numbers, return the largest overall", () => {
expect(findMax([-5, 10, -20, 3])).toBe(10);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("given only negative numbers, returns the one closest to zero", () => {
expect(findMax([-8, -2, -30])).toBe(-2);
})

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("given decimal numbers, returns the largest decimal", () => {
expect(findMax([1.5, 2.7, 2.65])).toBe(2.7);
})

// 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")
test("given an array with a non-number, throws an error", () => {
expect(() => findMax(["hey", 10, "hi", 60, 10])).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")
test("given something that is not an array, throws an error", () => {
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");
});
21 changes: 20 additions & 1 deletion Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
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 total = 0;

for (let i = 0; i < list.length; i++) {
if (typeof list[i] !== "number") {
throw new Error("calculateMean requires an array of numbers");
}
total += list[i];
}

return total / list.length;
}

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

// Given an array with a single number
// When passed to calculateMean
// Then it should return that number
test("given [7], return 7", () => {
expect(calculateMean([7])).toBe(7);
});

// Given an array containing negative or decimal numbers
// When passed to calculateMean
// Then it should return the correct mean
test("given an array of negative numbers, return the correct mean", () => {
expect(calculateMean([-1, -3, -5])).toBe(-3);
});

test("given an array of decimal numbers, return the correct mean", () => {
expect(calculateMean([1.5, 2.5])).toBe(2);
expect(calculateMean([0.1, 0.2])).toBeCloseTo(0.15);
});

// Given an empty array
// When passed to calculateMean
// Then it should throw Error("calculateMean requires a non-empty array")
test("given an empty array, throws an error", () => {
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")
test("given a value that is not an array, throws an error", () => {
expect(() => calculateMean("banana")).toThrow("calculateMean requires an array of numbers");
});

test("given a value that is not an array, throws an error", () => {
expect(() => calculateMean(42)).toThrow("calculateMean requires an array of numbers");
});

test("given a value that is not an array, throws an error", () => {
expect(() => calculateMean(null)).toThrow("calculateMean requires an array of numbers");
});

test("given a value that is not an array, throws an error", () => {
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")
test("given no argument at all, throws an error", () => {
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")
test("given an array with a non-number, throws an error", () => {
expect(() => calculateMean([7, "2", 5])).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 total = 0;

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

return total;
}

module.exports = sum;
23 changes: 22 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,45 @@ 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")
test("given an empty array, returns 0", () => {
expect(sum([])).toBe(0);
});

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

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

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("given an array with decimal numbers, returns the correct total", () => {
expect(sum([1.5, 2.5, 3])).toBe(7);
expect(sum([0.1, 0.2])).toBeCloseTo(0.3);
});

// 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")
test("given an array with a non-number, throws an error", () => {
expect(() => sum(["hey", 10, "hi", 60, 10])).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")
test("given something that is not an array, throws an error", () => {
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");
});
3 changes: 1 addition & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// 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];
for (const element of list) {
if (element === target) {
return true;
}
Expand Down
Loading