From 5decc5578df73b4c03866712cbbd2db915bb4482 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Fri, 4 Jul 2025 10:27:39 +0100 Subject: [PATCH 01/39] Refactor calculateMedian function to ensure input is an array before processing --- Sprint-1/fix/median.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..3a3ba1581 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,6 +6,9 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { + // checks if "list" is an array if not return's null. + if(!Array.isArray(list)) return null; + const middleIndex = Math.floor(list.length / 2); const median = list.splice(middleIndex, 1)[0]; return median; From 5b12f55e14276550ea4eb707e964bbf4234e9f3b Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Fri, 4 Jul 2025 10:38:34 +0100 Subject: [PATCH 02/39] Fix calculateMedian function to filter out non-number values from the input array --- Sprint-1/fix/median.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 3a3ba1581..27c45f5e9 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -8,6 +8,9 @@ function calculateMedian(list) { // checks if "list" is an array if not return's null. if(!Array.isArray(list)) return null; + + // filter the non number values out of an array like "strings", "null's and undefined" + const numbers = list.filter( value => typeof value === `number` && !isNaN(value)); const middleIndex = Math.floor(list.length / 2); const median = list.splice(middleIndex, 1)[0]; From 1c395d6c2a9db92bdae74b376b321a44cbecf430 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Fri, 4 Jul 2025 10:51:38 +0100 Subject: [PATCH 03/39] Fix calculateMedian function to correctly compute the median value from filtered numbers --- Sprint-1/fix/median.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 27c45f5e9..4363d3f34 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -11,10 +11,16 @@ function calculateMedian(list) { // filter the non number values out of an array like "strings", "null's and undefined" const numbers = list.filter( value => typeof value === `number` && !isNaN(value)); - + + // finds the middle index based on filtered numbers length const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; -} -module.exports = calculateMedian; + // return the correct median based on if its even or odd + if (numbers.length % 2 === 0){ + // even length average of two middle numbers + return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; + } else { + // odd length returns the middle number + return numbers[middleIndex]; + } +} \ No newline at end of file From 11a010f1e0db730764cac62a41a9d18c13d8d908 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Fri, 4 Jul 2025 10:53:21 +0100 Subject: [PATCH 04/39] Add check to return null if no valid numbers are present in calculateMedian function --- Sprint-1/fix/median.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 4363d3f34..964cf9216 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -12,6 +12,9 @@ function calculateMedian(list) { // filter the non number values out of an array like "strings", "null's and undefined" const numbers = list.filter( value => typeof value === `number` && !isNaN(value)); + //if no numbers are left return null + if (numbers.length === 0) return null; + // finds the middle index based on filtered numbers length const middleIndex = Math.floor(list.length / 2); From 589fef74c1ae279f1eb374d16928986a53aae16c Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Fri, 4 Jul 2025 10:56:23 +0100 Subject: [PATCH 05/39] Fix calculateMedian function to ensure sorting occurs before finding the median --- Sprint-1/fix/median.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 964cf9216..de4253d2d 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -12,9 +12,14 @@ function calculateMedian(list) { // filter the non number values out of an array like "strings", "null's and undefined" const numbers = list.filter( value => typeof value === `number` && !isNaN(value)); - //if no numbers are left return null + // if no numbers are left return null if (numbers.length === 0) return null; + // sort numbers into ascending order + numbers.sort(function(a, b){ + return a - b; + }) + // finds the middle index based on filtered numbers length const middleIndex = Math.floor(list.length / 2); From fc0d9bca997428683da5d932f1acdbc79d3ae94a Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Fri, 4 Jul 2025 12:00:02 +0100 Subject: [PATCH 06/39] Refactor includes function to use for...of loop for improved readability --- Sprint-1/refactor/includes.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..5beeaf0e2 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,12 +1,15 @@ // 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]; +// iterate through each element in the list + for (const element of list){ + + // If an element matches the target, return true if (element === target) { return true; } } + // If no element matches the target, return false return false; } From 4199529614cc078fc9aa03a54689ba7e043c5631 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Fri, 4 Jul 2025 23:20:25 +0100 Subject: [PATCH 07/39] implement: initialize newArray ready to store unique values for deduplication --- Sprint-1/implement/dedupe.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..849e54a47 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,4 @@ -function dedupe() {} +function dedupe() { + // makes a new array to store non duplicate values + const newArray = []; +} From df0b19c9cc18675a91acf0b2676411dbf325d78f Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Fri, 4 Jul 2025 23:36:11 +0100 Subject: [PATCH 08/39] feat: implement for...of loop to iterate through input array --- Sprint-1/implement/dedupe.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 849e54a47..66fcfaf12 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,4 +1,8 @@ function dedupe() { - // makes a new array to store non duplicate values - const newArray = []; + // makes a new array to store non duplicate values + const newArray = []; + + // loops over each item in the original array + for (const item of array) { + } } From a93c361a7d23e4adb376c9846921db939a39be0a Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Fri, 4 Jul 2025 23:40:32 +0100 Subject: [PATCH 09/39] added check to prevent duplicate values using .includes() --- Sprint-1/implement/dedupe.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 66fcfaf12..96b41d890 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -4,5 +4,8 @@ function dedupe() { // loops over each item in the original array for (const item of array) { + // checks if the values are not in the "newArray" + if (!newArray.includes(item)) { + } } } From 88ef0536fac2b1d4aa83a4ef8c0fd963fb0d35c8 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Fri, 4 Jul 2025 23:47:28 +0100 Subject: [PATCH 10/39] implemented: push non-duplicate items to unique array and return the newArray output --- Sprint-1/implement/dedupe.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 96b41d890..ab52db478 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -6,6 +6,10 @@ function dedupe() { for (const item of array) { // checks if the values are not in the "newArray" if (!newArray.includes(item)) { + // if item is not in "newArray" adds it to the array + newArray.push(item); } } + // returns the new deduped array + return newArray; } From 00abcc47237170c9467365af9ba6f72a0847591a Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Fri, 4 Jul 2025 23:58:14 +0100 Subject: [PATCH 11/39] fix: add missing array parameter to dedupe function --- Sprint-1/implement/dedupe.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index ab52db478..24e827196 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,4 +1,4 @@ -function dedupe() { +function dedupe(array) { // makes a new array to store non duplicate values const newArray = []; @@ -13,3 +13,4 @@ function dedupe() { // returns the new deduped array return newArray; } +console.log(dedupe([5, 1, 1, 2, 3, 2, 5, 8]))// expect [5, 1, 2, 3, 8] \ No newline at end of file From 1e3002d6baf9f60717dd8fcb821918a892985a69 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 5 Jul 2025 00:08:31 +0100 Subject: [PATCH 12/39] refactored: removed console.log --- Sprint-1/implement/dedupe.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 24e827196..f3975fc1e 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -12,5 +12,4 @@ function dedupe(array) { } // returns the new deduped array return newArray; -} -console.log(dedupe([5, 1, 1, 2, 3, 2, 5, 8]))// expect [5, 1, 2, 3, 8] \ No newline at end of file +} \ No newline at end of file From d5cc4cfc7f69f7606186131e797ca243606381b5 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 5 Jul 2025 00:13:53 +0100 Subject: [PATCH 13/39] reimplmented the module.exports for tests --- Sprint-1/implement/dedupe.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index f3975fc1e..340589dfe 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -12,4 +12,6 @@ function dedupe(array) { } // returns the new deduped array return newArray; -} \ No newline at end of file +} + +module.exports = dedupe; \ No newline at end of file From 5fde14967f1a5e5cd603597fa029f4bd42c28869 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 5 Jul 2025 00:17:16 +0100 Subject: [PATCH 14/39] implemented tests for giving the function an array with no duplicates and empty arrays --- Sprint-1/implement/dedupe.test.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..b31da6f0e 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,11 +16,16 @@ E.g. dedupe([1, 2, 1]) target output: [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", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + expect(dedupe(['a', 'b', 'c'])).toEqual(['a', 'b', 'c']); +}); // Given an array with strings or numbers // When passed to the dedupe function From b07f167082d4a92be72b0f50d25c9b689c1b01de Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 5 Jul 2025 11:15:35 +0100 Subject: [PATCH 15/39] add unit tests for dedupe function to ensure it removes duplicates while preserving first occurrences --- Sprint-1/implement/dedupe.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index b31da6f0e..7ab3903f8 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -30,3 +30,8 @@ test("given an array with no duplicates, it returns a copy of the original array // Given an array with strings or numbers // When passed to the dedupe function // Then it should remove the duplicate values, preserving the first occurence of each element +test("given an array with duplicates it removes any duplicates and preserves the first instance of each element", () => { + 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]); +}); \ No newline at end of file From 128a345be3bd0de35f678f1586d88c5bf394e5b4 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Wed, 9 Jul 2025 17:55:55 +0100 Subject: [PATCH 16/39] feat: implement findMax function to filter numbers from an array --- Sprint-1/implement/max.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..4c1ab1e63 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,15 @@ function findMax(elements) { + // makes an array to hold only numbers + const numbersOnly = [] + + // to loop through each element in the array + for (const item of elements){ + + // if the item is a number push it to the numbersOnly array + if (typeof item === `number`){ + numbersOnly.push(item); + } + } } module.exports = findMax; From 05d5b0180e15baac8493f6d44f3e7fdaea7e2341 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Wed, 9 Jul 2025 18:21:13 +0100 Subject: [PATCH 17/39] implemented findMax function to return the maximum number from an array --- Sprint-1/implement/max.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 4c1ab1e63..f943657c6 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -10,6 +10,17 @@ function findMax(elements) { numbersOnly.push(item); } } -} + // start with the first number in the "numbersOnly" array + let max = numbersOnly[0]; + // loop for the largest number in the "numbersOnly" array + for (const num of numbersOnly) { + if (num > max) { + max = num; + } + } + return max; +} +console.log(findMax([30, 50, 10, 40])); // 50 +console.log(findMax(['hey', 10, 'hi', 60, 10])); // 60 module.exports = findMax; From f630baab7a4d7dae084f3e34caa0e22c7ace8cc6 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Wed, 9 Jul 2025 18:30:00 +0100 Subject: [PATCH 18/39] implemented findMax function to return -Infinity when array is empty --- Sprint-1/implement/max.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index f943657c6..762f441d8 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -20,7 +20,13 @@ function findMax(elements) { } } return max; + + // if the no numbers are found in the array returns -infinty + if (numbersOnly.length === 0) { + return -Infinity; + } } console.log(findMax([30, 50, 10, 40])); // 50 console.log(findMax(['hey', 10, 'hi', 60, 10])); // 60 +console.log(findMax([])); // -infinity module.exports = findMax; From 2235414b1280640d5fb4e6bb05b95c32f70742fc Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Wed, 9 Jul 2025 18:39:12 +0100 Subject: [PATCH 19/39] fix: move empty array check to the correct position in findMax function --- Sprint-1/implement/max.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 762f441d8..8b2c2eada 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -13,6 +13,11 @@ function findMax(elements) { // start with the first number in the "numbersOnly" array let max = numbersOnly[0]; + // if the no numbers are found in the array returns -infinty + if (numbersOnly.length === 0) { + return -Infinity; + } + // loop for the largest number in the "numbersOnly" array for (const num of numbersOnly) { if (num > max) { @@ -21,10 +26,6 @@ function findMax(elements) { } return max; - // if the no numbers are found in the array returns -infinty - if (numbersOnly.length === 0) { - return -Infinity; - } } console.log(findMax([30, 50, 10, 40])); // 50 console.log(findMax(['hey', 10, 'hi', 60, 10])); // 60 From 4bcc17150c5ff5cf074175c16dfab949b8330861 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Wed, 9 Jul 2025 18:50:49 +0100 Subject: [PATCH 20/39] updated test for findMax function when given an empty array --- Sprint-1/implement/max.test.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..bb927427d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,8 +16,9 @@ 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 From f84d2f22f2d3231d39ce548a9c8b0931b83e1dcd Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Wed, 9 Jul 2025 18:59:22 +0100 Subject: [PATCH 21/39] added test a array has one number in and returns that number --- Sprint-1/implement/max.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index bb927427d..68926916c 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -22,6 +22,9 @@ test("given an empty array, returns -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 one number", () => { + expect(findMax([42])).toBe(42); +}); // Given an array with both positive and negative numbers // When passed to the max function From e78614b60a2ae8882f66427ef4c59db2c2c3b374 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Wed, 9 Jul 2025 19:04:00 +0100 Subject: [PATCH 22/39] added jest Test given an array with both positive and negative numbers, returns the largest --- Sprint-1/implement/max.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 68926916c..0aaad3bba 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -29,6 +29,9 @@ test("given an array with one number returns that one number", () => { // 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 an array with both positive and negative numbers returns the largest", () => { + expect(findMax([-10, 5, -30, 27])).toBe(27); +}) // Given an array with just negative numbers // When passed to the max function From ff06a576f778bf377a31c2a6e9a4e6c0adc85572 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Wed, 9 Jul 2025 19:12:44 +0100 Subject: [PATCH 23/39] added jest Test: given an array with just negative numbers, returns the closest one to zero --- Sprint-1/implement/max.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 0aaad3bba..46c0ca3cf 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -36,6 +36,9 @@ test("given an array with both positive and negative numbers returns the largest // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("given an array with just negative numbers, returns the closest one to zero", () => { + expect(findMax([-100, -10, -5, -50])).toBe(-5); +}); // Given an array with decimal numbers // When passed to the max function From f0d4e09c7a81eaa999813c7ccd733781831586bd Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Wed, 9 Jul 2025 19:18:14 +0100 Subject: [PATCH 24/39] added jest Test: given an array with decimal numbers, returns the largest decimal number --- Sprint-1/implement/max.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 46c0ca3cf..a1079a21a 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -43,6 +43,9 @@ test("given an array with just negative numbers, returns 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 +test("given an array with decimal numbers, returns the largest decimal", () => { + expect(findMax([1.5, 2.3, 0.7, ,3.14, 2.71])).toBe(3.14); +}); // Given an array with non-number values // When passed to the max function From 426d20526b194e74fb743286c542378ef1023760 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Wed, 9 Jul 2025 19:21:11 +0100 Subject: [PATCH 25/39] added jest test: given an array with non-number values, returns the max and ignores non-numeric values --- Sprint-1/implement/max.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index a1079a21a..64ab1e702 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -50,6 +50,9 @@ test("given an array with decimal numbers, returns the largest decimal", () => { // Given an array with non-number values // When passed to the max function // Then it should return the max and ignore non-numeric values +test("given an array with non-number values, returns the max and removes the non-numeric values", () => { + expect(findMax([`hey`, 10, `hi`, 60, 10])).toBe(60); +}) // Given an array with only non-number values // When passed to the max function From 4221f01eafb5d4b7296d58ca004cd38b5587d0d2 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Wed, 9 Jul 2025 19:23:38 +0100 Subject: [PATCH 26/39] added jest test: given an array with only non-number values, returns -Infinity --- Sprint-1/implement/max.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 64ab1e702..81f185eda 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -57,3 +57,6 @@ test("given an array with non-number values, returns the max and removes the non // Given an array with only non-number values // When passed to the max function // Then it should return the least surprising value given how it behaves for all other inputs +test("given an array with only non-number values, returns -Infinity", () => { + expect(findMax([`hey`, `hi`, `hello`])).toBe(-Infinity); +}) \ No newline at end of file From b65624737b1e768e0237e059e7d1636223ef8ad4 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 12 Jul 2025 09:04:11 +0100 Subject: [PATCH 27/39] Add check to return 0 if input array is empty in sum function --- Sprint-1/implement/sum.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..cac79ace3 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,6 @@ function sum(elements) { + if (elements.length === 0) return 0; // if array is input is empty returns 0 + } module.exports = sum; From 5e6208529a6e8af2f4ee0728905a29a1ac0635a7 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 12 Jul 2025 09:06:45 +0100 Subject: [PATCH 28/39] added jest test for if given an array is empty --- Sprint-1/implement/sum.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..b269bcb1b 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,7 +13,9 @@ 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 From de951dfe56ec866201eb78629854ea966ee83bca Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 12 Jul 2025 09:50:59 +0100 Subject: [PATCH 29/39] added for...of loop for to check elements in the arrays are numbers and adds numbers to total sum --- Sprint-1/implement/sum.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index cac79ace3..46da635f2 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,6 +1,17 @@ function sum(elements) { if (elements.length === 0) return 0; // if array is input is empty returns 0 - + + let total = 0; // create a variable to hold the sum + + // iterate over each element in the array + for (const value of elements){ + // checks if each of the values are numbers + if (typeof value === `number`){ + // adds the numbers to the total sum + total += value; + } + } +return total; // returns the total sum after the loop has finished adding all the numbers } module.exports = sum; From d87cea0f386a4dbd72627b7d84e46b9de823b43b Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 12 Jul 2025 09:51:51 +0100 Subject: [PATCH 30/39] added jest test for if an array has just one number --- Sprint-1/implement/sum.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index b269bcb1b..db79b4d24 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -20,6 +20,9 @@ test("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 +test("given an array with a single number return that number", () => { + expect(sum([42])).toBe(42); +}) // Given an array containing negative numbers // When passed to the sum function From 0ab4dfeda635ae9383fdb8b7b450342a73861a33 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 12 Jul 2025 09:55:20 +0100 Subject: [PATCH 31/39] added jest tests for if an array contains negative numbers --- Sprint-1/implement/sum.test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index db79b4d24..9154ba000 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -27,7 +27,10 @@ test("given an array with a single number return that number", () => { // 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 sum", () => { + expect(sum([-10, -20, -30])).toBe(-60); + expect(sum([-10, 20, -30])).toBe(-20); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum From 93778e0fb2336dcc94b5b2b282bcb590d40b70a5 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 12 Jul 2025 10:04:37 +0100 Subject: [PATCH 32/39] added jest test for when an array has decima/float numbers --- Sprint-1/implement/sum.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 9154ba000..4bbf7a388 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -34,6 +34,10 @@ test("given an array with negative numbers returns 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 +test("given an array with decimal numbers in returns the correct total sum", () => { + expect(sum([10.5, 20.2, 30.3])).toBe(61.0); + expect(sum([10.5, -20.2, 30.3])).toBe(20.6); +}); // Given an array containing non-number values // When passed to the sum function From cabb9a6fb7dee7ee1a4f0aeff5e9004946c4ed81 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 12 Jul 2025 10:10:06 +0100 Subject: [PATCH 33/39] added jest test for if an array contains non-numerical values --- Sprint-1/implement/sum.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 4bbf7a388..311fa0a1e 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -42,6 +42,9 @@ test("given an array with decimal numbers in returns the correct total sum", () // Given an array containing non-number values // When passed to the sum function // Then it should ignore the non-numerical values and return the sum of the numerical elements +test("given an array containing non-numerical values ignore non-numerical and returns correct total sum", () => { + expect(sum([2, "hello", 26.5, "hey", -6.6, 8])).toBe(29.9); +}); // Given an array with only non-number values // When passed to the sum function From da4dd6c91a027dfbe1bc7b78145c6a24171df30d Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 12 Jul 2025 10:17:12 +0100 Subject: [PATCH 34/39] added jest test for when an array has only non-numerical values --- Sprint-1/implement/sum.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 311fa0a1e..60cf5f356 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -49,3 +49,6 @@ test("given an array containing non-numerical values ignore non-numerical and re // Given an array with only non-number values // When passed to the sum function // Then it should return the least surprising value given how it behaves for all other inputs +test("given an array with only non-numerical values returns 0", () => { + expect(sum(["a", "b", "c"])).toBe(0); +}); From 36aee36b12003dc12f9c6512b2a5216a62aa954b Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 22 Nov 2025 12:46:32 +0000 Subject: [PATCH 35/39] refactor: improve median calculation by filtering non-finite values and enhancing code readability --- Sprint-1/fix/median.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index de4253d2d..3e28e6395 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -7,28 +7,28 @@ function calculateMedian(list) { // checks if "list" is an array if not return's null. - if(!Array.isArray(list)) return null; + if (!Array.isArray(list)) return null; - // filter the non number values out of an array like "strings", "null's and undefined" - const numbers = list.filter( value => typeof value === `number` && !isNaN(value)); + // filter the non number values out of an array like "strings", and checks for " Infinte, -Infinte, null's and undefined" + const numbers = list.filter(value => Number.isFinite(value)); // if no numbers are left return null if (numbers.length === 0) return null; // sort numbers into ascending order - numbers.sort(function(a, b){ + numbers.sort(function (a, b) { return a - b; - }) + }); // finds the middle index based on filtered numbers length const middleIndex = Math.floor(list.length / 2); // return the correct median based on if its even or odd - if (numbers.length % 2 === 0){ + if (numbers.length % 2 === 0) { // even length average of two middle numbers return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; } else { // odd length returns the middle number return numbers[middleIndex]; } -} \ No newline at end of file +} From db5390c77125ec882fd8ad55743160741132fea3 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 22 Nov 2025 14:44:22 +0000 Subject: [PATCH 36/39] added tests to check if the dedupe function makes an new array and not using the original array --- Sprint-1/implement/dedupe.test.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 7ab3903f8..1bba725c6 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -17,21 +17,27 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] // When passed to the dedupe function // Then it should return an empty array test("given an empty array, it returns an empty array", () => { -expect(dedupe([])).toEqual([]); + 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", () => { expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); - expect(dedupe(['a', 'b', 'c'])).toEqual(['a', 'b', 'c']); + expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]); + + const original = [1, 2, 3]; + const result = dedupe(original); + + expect(result).toEqual(original); + expect(result).not.toBe(original); }); // Given an array with strings or numbers // When passed to the dedupe function // Then it should remove the duplicate values, preserving the first occurence of each element test("given an array with duplicates it removes any duplicates and preserves the first instance of each element", () => { - expect(dedupe(['a','a','a','b','b','c'])).toEqual(['a','b','c']); + 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]); -}); \ No newline at end of file +}); From 205ef3d9c18e0a9efdf73e0cb7b359cc9f55a3e5 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 22 Nov 2025 14:48:20 +0000 Subject: [PATCH 37/39] refactor: improve code readability and formatting in findMax function --- Sprint-1/implement/max.js | 44 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 8b2c2eada..77595f102 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,33 +1,31 @@ function findMax(elements) { - // makes an array to hold only numbers - const numbersOnly = [] + // makes an array to hold only numbers + const numbersOnly = []; - // to loop through each element in the array - for (const item of elements){ - - // if the item is a number push it to the numbersOnly array - if (typeof item === `number`){ - numbersOnly.push(item); - } + // to loop through each element in the array + for (const item of elements) { + // if the item is a number push it to the numbersOnly array + if (typeof item === `number`) { + numbersOnly.push(item); } - // start with the first number in the "numbersOnly" array - let max = numbersOnly[0]; + } + // if the no numbers are found in the array returns -infinty + if (numbersOnly.length === 0) { + return -Infinity; + } - // if the no numbers are found in the array returns -infinty - if (numbersOnly.length === 0) { - return -Infinity; - } + // start with the first number in the "numbersOnly" array + let max = numbersOnly[0]; - // loop for the largest number in the "numbersOnly" array - for (const num of numbersOnly) { - if (num > max) { - max = num; - } + // loop for the largest number in the "numbersOnly" array + for (const num of numbersOnly) { + if (num > max) { + max = num; } - return max; - + } + return max; } console.log(findMax([30, 50, 10, 40])); // 50 -console.log(findMax(['hey', 10, 'hi', 60, 10])); // 60 +console.log(findMax(["hey", 10, "hi", 60, 10])); // 60 console.log(findMax([])); // -infinity module.exports = findMax; From 8b4c736c04a32a215a774242e946cde040e62f49 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 29 Nov 2025 11:50:57 +0000 Subject: [PATCH 38/39] fix: correct assertion method in test for non-numerical values in sum function --- Sprint-1/implement/sum.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 60cf5f356..52ef94376 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -43,7 +43,7 @@ test("given an array with decimal numbers in returns the correct total sum", () // When passed to the sum function // Then it should ignore the non-numerical values and return the sum of the numerical elements test("given an array containing non-numerical values ignore non-numerical and returns correct total sum", () => { - expect(sum([2, "hello", 26.5, "hey", -6.6, 8])).toBe(29.9); + expect(sum([2, "hello", 26.5, "hey", -6.6, 8])).toBeEqualTo(29.9); }); // Given an array with only non-number values From bc910d71e54ab2879b9adf8dae805ab619843c89 Mon Sep 17 00:00:00 2001 From: Declan Williams Date: Sat, 29 Nov 2025 12:18:44 +0000 Subject: [PATCH 39/39] fix: corrected .toBeCloseTo() --- Sprint-1/implement/sum.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 52ef94376..b60ee18c1 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -43,7 +43,7 @@ test("given an array with decimal numbers in returns the correct total sum", () // When passed to the sum function // Then it should ignore the non-numerical values and return the sum of the numerical elements test("given an array containing non-numerical values ignore non-numerical and returns correct total sum", () => { - expect(sum([2, "hello", 26.5, "hey", -6.6, 8])).toBeEqualTo(29.9); + expect(sum([2, "hello", 26.5, "hey", -6.6, 8])).toBeCloseTo(29.9); }); // Given an array with only non-number values