From 28f63cce3963f8666cacc71a26170f89e3bd3624 Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 1 Mar 2026 14:29:25 +0000 Subject: [PATCH 1/6] course work (a) --- Sprint-1/fix/median.js | 22 +++++++++++++++++---- Sprint-1/implement/dedupe.js | 7 ++++++- Sprint-1/implement/dedupe.test.js | 13 ++++++++++++- Sprint-1/implement/max.js | 6 ++++++ Sprint-1/implement/max.test.js | 32 ++++++++++++++++++++++++++++--- Sprint-1/implement/sum.js | 9 ++++++++- Sprint-1/implement/sum.test.js | 23 +++++++++++++++++++++- Sprint-1/refactor/includes.js | 6 +++--- 8 files changed, 104 insertions(+), 14 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..4745afeff 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,23 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + const numbersOnly = list.filter((element) => typeof element === "number"); + numbersOnly.sort((a, b) => a - b); + const middleIndex = Math.floor(numbersOnly.length / 2); + let middle = 0; + if (numbersOnly.length === 0) { + return null; + } + if (numbersOnly.length % 2 === 0) { + middle = numbersOnly[middleIndex]; + return middle; + } else { + let oneDown = numbersOnly.length / 2 - 1; + let oneUp = numbersOnly.length / 2; + middle = (numbersOnly[oneDown] + numbersOnly[oneUp]) / 2; + return middle; + } } - +// const numbers = elements.filter((element) => typeof element === "number"); +console.log(calculateMedian(["banana", 5, 3, "apple", 1, 4, 6, 2])); module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..043f3263d 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,6 @@ -function dedupe() {} +function dedupe(elements) { + const noDupe = [...new Set(elements)]; + return noDupe; +} +console.log(dedupe([3, 3, 3, 4, 5, 4, "cat", "dog", "cat", 6, 1, 2, 3, 1])); +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..a6fd5efdc 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,23 @@ 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 // Then it should remove the duplicate values, preserving the first occurence of each element +test("given an array with strings or numbers, it removes duplicates preserving first occurrence", () => { + expect(dedupe(["a", "a", "b", "c", "c"])).toEqual(["a", "b", "c"]); + expect(dedupe([1, 2, 2, 3, 4, 4])).toEqual([1, 2, 3, 4]); + expect(dedupe([4, 6, "hi", 4, 6, "hi"])).toEqual([4, 6, "hi"]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..92f3eb0bd 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,10 @@ function findMax(elements) { + const numbers = elements.filter((element) => typeof element === "number"); + if (numbers.length === 0) { + return -Infinity; + } + numbers.sort((a, b) => a - b); + return numbers[numbers.length - 1]; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..4285cebf1 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -15,29 +15,55 @@ 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"); +test("Given an empty array Then it should return -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([2])).toEqual(2); + expect(findMax([7])).toEqual(7); + expect(findMax([-7])).toEqual(-7); + expect(findMax([70000000])).toEqual(70000000); +}); // 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 Then it should return the largest number overall", () => { + expect(findMax([2, -9])).toEqual(2); + expect(findMax([2, -4, -2, 9])).toEqual(9); +}); // 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 Then it should return the closest one to zero", () => { + expect(findMax([-2, -4, -1, -99])).toEqual(-1); + expect(findMax([2])).toEqual(2); +}); // 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 just negative numbers Then it should return the closest one to zero", () => { + expect(findMax([3.7, 3.8, 3.9])).toEqual(3.9); + expect(findMax([2.8, 2.6, 2.5, 2.1])).toEqual(2.8); +}); // 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 just negative numbers Then it should return the closest one to zero", () => { + expect(findMax([-2, -4, -1, "p"])).toEqual(-1); + expect(findMax([2, 5, "i", 3, "r"])).toEqual(5); +}); // 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 +// Then it should return the least surprising value given how it behaves for all other input +test("Given an array with only non-number values Then it should return the least surprising value given how it behaves for all other input", () => { + expect(findMax([9])).toEqual(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..bf5242fd5 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,11 @@ function sum(elements) { -} + let total = 0; + const numbers = elements.filter((element) => typeof element === "number"); + numbers.forEach((numb) => { + total = total + numb; + }); + return total; +} +console.log(sum([5])); module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..6a3a159c6 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -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 Then it should return 0", () => { + expect(sum([])).toEqual(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 one element it should return that number", () => { + expect(sum([4])).toEqual(4); + expect(sum([10000])).toEqual(10000); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("Given negitave numbers still sums correctly", () => { + expect(sum([2, 3, -3])).toEqual(2); + expect(sum([5, -3, -3])).toEqual(-1); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("Given a float number it still sums correctly", () => { + expect(sum([4.2, 4.3])).toEqual(8.5); + expect(sum([2.223, 5.345, 3.141])).toEqual(10.709); + expect(sum([3.887, 2.1945, -3.5])).toEqual(2.5815); +}); // 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 non-numerical values should ignore and sum the numbers", () => { + expect(sum([4, "four", "fore", "iv", 4])).toEqual(8); +}); // 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 only non-numerical values should return 0", () => { + expect(sum(["tommorow", "and", "tommorow", "and", "tommorow"])); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..e6f1b0f55 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,9 +1,9 @@ // 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 (let i = 0; i < list.length; i++) { + const element = list[i]; + if (element == target) { return true; } } From 0086bf5536818fb4cca03f6c8f519aa015d6cb51 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 2 Mar 2026 11:10:16 +0000 Subject: [PATCH 2/6] fix still needs work --- Sprint-1/fix/median.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 4745afeff..068efbd26 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,21 +6,17 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - const numbersOnly = list.filter((element) => typeof element === "number"); - numbersOnly.sort((a, b) => a - b); - const middleIndex = Math.floor(numbersOnly.length / 2); - let middle = 0; - if (numbersOnly.length === 0) { + list = list.filter((element) => typeof element === "number"); + if (list.length === 0) { return null; - } - if (numbersOnly.length % 2 === 0) { - middle = numbersOnly[middleIndex]; - return middle; } else { - let oneDown = numbersOnly.length / 2 - 1; - let oneUp = numbersOnly.length / 2; - middle = (numbersOnly[oneDown] + numbersOnly[oneUp]) / 2; - return middle; + list.sort((a, b) => a - b); + const middleIndex = Math.floor(list.length / 2); + if (list.length % 2 === 0) { + return (list[middleIndex - 1] + list[middleIndex]) / 2; + } else { + return list[middleIndex]; + } } } // const numbers = elements.filter((element) => typeof element === "number"); From c067a0d421b6dffc6f3a39285298230bfc213767 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 2 Mar 2026 21:12:27 +0000 Subject: [PATCH 3/6] still need to work on median --- Sprint-1/fix/median.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 068efbd26..ba5531ef0 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,19 +6,25 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - list = list.filter((element) => typeof element === "number"); - if (list.length === 0) { + let newOrder = list; + let i = 0; + while (i < newOrder.length) { + if (typeof newOrder[i] !== "number") newOrder.splice(i, 1); + else i++; + } + if (newOrder.length === 0) { return null; + } + newOrder = list.sort((a, b) => a - b); + const middleIndex = Math.floor(newOrder.length / 2); + if (newOrder.length % 2 !== 0) { + return newOrder[middleIndex]; } else { - list.sort((a, b) => a - b); - const middleIndex = Math.floor(list.length / 2); - if (list.length % 2 === 0) { - return (list[middleIndex - 1] + list[middleIndex]) / 2; - } else { - return list[middleIndex]; - } + const middleTwo = (newOrder[middleIndex] + newOrder[middleIndex - 1]) / 2; + return middleTwo; } } -// const numbers = elements.filter((element) => typeof element === "number"); -console.log(calculateMedian(["banana", 5, 3, "apple", 1, 4, 6, 2])); module.exports = calculateMedian; +console.log(calculateMedian([110, 20, 0])); +console.log(calculateMedian([3, "apple", 1, 2, undefined, 4])); +console.log(calculateMedian([])); From 0ebe10187d25493ee9465d8708f4daba9ba8aae3 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 4 Mar 2026 14:01:59 +0000 Subject: [PATCH 4/6] started now --- Sprint-1/implement/max.test.js | 32 +++----------------------------- Sprint-1/refactor/includes.js | 6 +++--- 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 4285cebf1..82f18fd88 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -15,55 +15,29 @@ const findMax = require("./max.js"); // Given an empty array // When passed to the max function // Then it should return -Infinity -test("Given an empty array Then it should return -Infinity", () => { - expect(findMax([])).toBe(-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 -test("given an array with one number, returns that number", () => { - expect(findMax([2])).toEqual(2); - expect(findMax([7])).toEqual(7); - expect(findMax([-7])).toEqual(-7); - expect(findMax([70000000])).toEqual(70000000); -}); // 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 Then it should return the largest number overall", () => { - expect(findMax([2, -9])).toEqual(2); - expect(findMax([2, -4, -2, 9])).toEqual(9); -}); // 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 Then it should return the closest one to zero", () => { - expect(findMax([-2, -4, -1, -99])).toEqual(-1); - expect(findMax([2])).toEqual(2); -}); // 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 just negative numbers Then it should return the closest one to zero", () => { - expect(findMax([3.7, 3.8, 3.9])).toEqual(3.9); - expect(findMax([2.8, 2.6, 2.5, 2.1])).toEqual(2.8); -}); // 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 just negative numbers Then it should return the closest one to zero", () => { - expect(findMax([-2, -4, -1, "p"])).toEqual(-1); - expect(findMax([2, 5, "i", 3, "r"])).toEqual(5); -}); // 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 input -test("Given an array with only non-number values Then it should return the least surprising value given how it behaves for all other input", () => { - expect(findMax([9])).toEqual(-Infinity); -}); +// Then it should return the least surprising value given how it behaves for all other inputs diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index e6f1b0f55..29dad81f0 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,9 +1,9 @@ // Refactor the implementation of includes to use a for...of loop function includes(list, target) { - for (let i = 0; i < list.length; i++) { - const element = list[i]; - if (element == target) { + for (let index = 0; index < list.length; index++) { + const element = list[index]; + if (element === target) { return true; } } From 120e80ed008bb8e2d17da9bfd77a540578f751d1 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 4 Mar 2026 14:03:45 +0000 Subject: [PATCH 5/6] first fix now works --- Sprint-1/fix/median.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index ba5531ef0..d2b910fe3 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,7 +6,8 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - let newOrder = list; + list = list || []; + let newOrder = Array.from(list); let i = 0; while (i < newOrder.length) { if (typeof newOrder[i] !== "number") newOrder.splice(i, 1); @@ -15,7 +16,7 @@ function calculateMedian(list) { if (newOrder.length === 0) { return null; } - newOrder = list.sort((a, b) => a - b); + newOrder = newOrder.sort((a, b) => a - b); const middleIndex = Math.floor(newOrder.length / 2); if (newOrder.length % 2 !== 0) { return newOrder[middleIndex]; @@ -28,3 +29,4 @@ module.exports = calculateMedian; console.log(calculateMedian([110, 20, 0])); console.log(calculateMedian([3, "apple", 1, 2, undefined, 4])); console.log(calculateMedian([])); +console.log(calculateMedian(["frog", "dog", "log", "cog"])); From c65cbefa13a1ffebabe4350e20f216cafb35ba26 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 4 Mar 2026 14:33:27 +0000 Subject: [PATCH 6/6] sprint 1 course work --- Sprint-1/implement/max.test.js | 34 +++++++++++++++++++++++++++++++--- Sprint-1/refactor/includes.js | 3 +-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..0b44a2c11 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -15,29 +15,57 @@ 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"); +test("Given an empty array Then it should return -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([2])).toEqual(2); + expect(findMax([7])).toEqual(7); + expect(findMax([-7])).toEqual(-7); + expect(findMax([70000000])).toEqual(70000000); +}); // 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 Then it should return the largest number overall", () => { + expect(findMax([2, -9])).toEqual(2); + expect(findMax([2, -4, -2, 9])).toEqual(9); +}); // 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 Then it should return the closest one to zero", () => { + expect(findMax([-2, -4, -1, -99])).toEqual(-1); + expect(findMax([2])).toEqual(2); +}); // 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 just negative numbers Then it should return the closest one to zero", () => { + expect(findMax([3.7, 3.8, 3.9])).toEqual(3.9); + expect(findMax([2.8, 2.6, 2.5, 2.1])).toEqual(2.8); +}); // 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 just negative numbers Then it should return the closest one to zero", () => { + expect(findMax([-2, -4, -1, "p"])).toEqual(-1); + expect(findMax([2, 5, "i", 3, "r"])).toEqual(5); +}); // 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 +// Then it should return the least surprising value given how it behaves for all other input +test("Given an array with only non-number values Then it should return the least surprising value given how it behaves for all other input", () => { + expect(findMax(["cat"])).toEqual(-Infinity); + expect(findMax(["cat", "dog"])).toEqual(-Infinity); + expect(findMax(["cat", "dog", "catdog"])).toEqual(-Infinity); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8c9ae2e66 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -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; }