From 03bb1b12b59cc6c2de2a7b97ef16470f1bad3821 Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Sun, 29 Mar 2026 00:49:31 +0100 Subject: [PATCH 1/7] Add prep folder to the main branch --- Prep/mean.js | 20 ++++++++++++++++++++ Prep/mean.test.js | 9 +++++++++ Prep/median.js | 6 ++++++ Prep/median.test.js | 0 Prep/summation.js | 21 +++++++++++++++++++++ Prep/summation.test.js | 9 +++++++++ 6 files changed, 65 insertions(+) create mode 100644 Prep/mean.js create mode 100644 Prep/mean.test.js create mode 100644 Prep/median.js create mode 100644 Prep/median.test.js create mode 100644 Prep/summation.js create mode 100644 Prep/summation.test.js diff --git a/Prep/mean.js b/Prep/mean.js new file mode 100644 index 000000000..57e562338 --- /dev/null +++ b/Prep/mean.js @@ -0,0 +1,20 @@ +// function calculateMean(list) { +// const total = list[0] + list[1] + list[2]; +// const mean = total / list.length; + +// return mean; +// } + +// Running the same code with a loop function inside the calculateMean function. + +function calculateMean(list) { + let total = 0; + let i = 0; + + for (let i = 0; i < list.length; i++) { + total = total + list[i]; + } + return total / list.length; +} + +module.exports = calculateMean; diff --git a/Prep/mean.test.js b/Prep/mean.test.js new file mode 100644 index 000000000..d515c1dff --- /dev/null +++ b/Prep/mean.test.js @@ -0,0 +1,9 @@ +const calculateMean = require("./mean"); + +test("calculates the mean of a list of numbers", () => { + const list = [3, 50, 7]; + const currentOutput = calculateMean(list); + const targetOutput = 20; + + expect(currentOutput).toEqual(targetOutput); // 20 is (3 + 50 + 7) / 3 +}); diff --git a/Prep/median.js b/Prep/median.js new file mode 100644 index 000000000..80a27fa99 --- /dev/null +++ b/Prep/median.js @@ -0,0 +1,6 @@ +function calculateMedian(list) { + const middleIndex = Math.floor(list.length / 2); + const median = list.splice(middleIndex, 1)[0]; + + return median; +} diff --git a/Prep/median.test.js b/Prep/median.test.js new file mode 100644 index 000000000..e69de29bb diff --git a/Prep/summation.js b/Prep/summation.js new file mode 100644 index 000000000..ee4897184 --- /dev/null +++ b/Prep/summation.js @@ -0,0 +1,21 @@ +// function sumValues(list) { +// let total = 0; +// total += list[0]; // access a list element and add to total +// total += list[1]; +// total += list[2]; +// total += list[3]; +// total += list[4]; +// return total; +// } + +// sumValues([1, 2, 3, 4, 5]); + +function sumValues(list) { + let total = 0; + for (i = 0; i < list.length; i++) { + total = total += list[i]; + } + return total; +} + +module.exports = sumValues; diff --git a/Prep/summation.test.js b/Prep/summation.test.js new file mode 100644 index 000000000..4724ce58e --- /dev/null +++ b/Prep/summation.test.js @@ -0,0 +1,9 @@ +const sumValues = require("./summation"); + +test("calculates the mean of a list of numbers", () => { + const list = [3, 50, 7]; + const currentOutput = sumValues(list); + const targetOutput = 60; + + expect(currentOutput).toEqual(targetOutput); +}); From 36579c9595b1f47fdf3a2a46954f9d99ab7c8b89 Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Sun, 29 Mar 2026 00:50:37 +0100 Subject: [PATCH 2/7] Removed Prep folder from coursework/sprint-1 --- Prep/mean.js | 20 -------------------- Prep/mean.test.js | 9 --------- Prep/median.js | 6 ------ Prep/median.test.js | 0 Prep/summation.js | 21 --------------------- Prep/summation.test.js | 9 --------- 6 files changed, 65 deletions(-) delete mode 100644 Prep/mean.js delete mode 100644 Prep/mean.test.js delete mode 100644 Prep/median.js delete mode 100644 Prep/median.test.js delete mode 100644 Prep/summation.js delete mode 100644 Prep/summation.test.js diff --git a/Prep/mean.js b/Prep/mean.js deleted file mode 100644 index 57e562338..000000000 --- a/Prep/mean.js +++ /dev/null @@ -1,20 +0,0 @@ -// function calculateMean(list) { -// const total = list[0] + list[1] + list[2]; -// const mean = total / list.length; - -// return mean; -// } - -// Running the same code with a loop function inside the calculateMean function. - -function calculateMean(list) { - let total = 0; - let i = 0; - - for (let i = 0; i < list.length; i++) { - total = total + list[i]; - } - return total / list.length; -} - -module.exports = calculateMean; diff --git a/Prep/mean.test.js b/Prep/mean.test.js deleted file mode 100644 index d515c1dff..000000000 --- a/Prep/mean.test.js +++ /dev/null @@ -1,9 +0,0 @@ -const calculateMean = require("./mean"); - -test("calculates the mean of a list of numbers", () => { - const list = [3, 50, 7]; - const currentOutput = calculateMean(list); - const targetOutput = 20; - - expect(currentOutput).toEqual(targetOutput); // 20 is (3 + 50 + 7) / 3 -}); diff --git a/Prep/median.js b/Prep/median.js deleted file mode 100644 index 80a27fa99..000000000 --- a/Prep/median.js +++ /dev/null @@ -1,6 +0,0 @@ -function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - - return median; -} diff --git a/Prep/median.test.js b/Prep/median.test.js deleted file mode 100644 index e69de29bb..000000000 diff --git a/Prep/summation.js b/Prep/summation.js deleted file mode 100644 index ee4897184..000000000 --- a/Prep/summation.js +++ /dev/null @@ -1,21 +0,0 @@ -// function sumValues(list) { -// let total = 0; -// total += list[0]; // access a list element and add to total -// total += list[1]; -// total += list[2]; -// total += list[3]; -// total += list[4]; -// return total; -// } - -// sumValues([1, 2, 3, 4, 5]); - -function sumValues(list) { - let total = 0; - for (i = 0; i < list.length; i++) { - total = total += list[i]; - } - return total; -} - -module.exports = sumValues; diff --git a/Prep/summation.test.js b/Prep/summation.test.js deleted file mode 100644 index 4724ce58e..000000000 --- a/Prep/summation.test.js +++ /dev/null @@ -1,9 +0,0 @@ -const sumValues = require("./summation"); - -test("calculates the mean of a list of numbers", () => { - const list = [3, 50, 7]; - const currentOutput = sumValues(list); - const targetOutput = 60; - - expect(currentOutput).toEqual(targetOutput); -}); From 6d567bb43e3afc84b2785047fd0277d86aac90e5 Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Sun, 5 Apr 2026 15:58:04 +0200 Subject: [PATCH 3/7] Fix median implementation --- Sprint-1/fix/median.js | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..1f95cf408 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,10 +5,45 @@ // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) // or 'list' has mixed values (the function is expected to sort only numbers). +// function calculateMedian(list) { +// const middleIndex = Math.floor(list.length / 2); // Finds the middle index of the array (rounds down if even) +// const median = list.splice(middleIndex, 1)[0]; // Removes the value at the middle index from the array and gets that value +// return median; // Returns the value found above as the median +// } + function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (!Array.isArray(list)) { + // We first check if the array is indeed an array. + return null; + } + + let numbers = []; + for (let i = 0; i < list.length; i++) { + if (typeof list[i] === "number") { + // Checks if the item in the array is a number, if so, push it to the numbers array. + numbers.push(list[i]); + } + } + if (numbers.length === 0) { + // This part checks if the numbers array we created is empty. If so, we return null. + return null; + } + + let sorted = numbers.slice(); + sorted.sort(function (a, b) { + // sorted is the sorted version of the numbers array. We created a new array so we don't lose the original. + return a - b; + }); + + let numbersAmount = sorted.length; + if (numbersAmount % 2 === 1) { + // Checks if the numbers in the array is odd. For example 3 numbers in the array. + return sorted[Math.floor(n / 2)]; // Returns the middle number of the array. We count from 0 so for example 0, 1, "2", 3, 4. + } else { + let mid1 = sorted[n / 2 - 1]; + let mid2 = sorted[n / 2]; + return (mid1 + mid2) / 2; // We take the two middle numbers avarege with divide by 2 to get the median. + } } module.exports = calculateMedian; From 883afa58a4474945b039f7d1cd897d739a596c9e Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Sun, 5 Apr 2026 15:58:18 +0200 Subject: [PATCH 4/7] Implement sum and add tests --- Sprint-1/implement/sum.js | 9 +++++++++ Sprint-1/implement/sum.test.js | 25 ++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..75322b173 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,13 @@ function sum(elements) { + let total = 0; + + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number") { + total = total + elements[i]; + } + } + + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..ac455cc34 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,47 @@ 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 one number, returns that number", () => { + expect(sum([44])).toBe(44); +}); + // 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 containing negative numbers, returns the correct total sum", () => { + expect(sum([-10, 5, -3, 8])).toBe(0); +}); + // 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 sum", () => { + expect(sum([1.5, 2.5, 3.5])).toBe(7.5); +}); + // 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-number values, ignores them and returns the sum of numbers", () => { + expect(sum(["hey", 10, "hi", 60, 10])).toBe(80); +}); + // 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-number values, returns 0", () => { + expect(sum(["hello", null, undefined, "banana"])).toBe(0); +}); From d8e7ba3f18af230a768d4a8fe3fc6e82ecd8cb9e Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Sun, 5 Apr 2026 15:58:29 +0200 Subject: [PATCH 5/7] Implement max and add tests --- Sprint-1/implement/max.js | 13 +++++++++++- Sprint-1/implement/max.test.js | 37 +++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..6e9018284 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,15 @@ -function findMax(elements) { +function findMax(arrayElements) { + let max = -Infinity; // define max first, if the array is empty, return -Infinity + for (i = 0; i < arrayElements.length; i++) { + if (typeof arrayElements[i] === "number") { + // first checks if the array element is a number before moving on. + if (arrayElements[i] > max) { + // Finds the largest number after the === numbers test. + max = arrayElements[i]; // redefine max with the maximum number in the array. + } + } + } + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..22d8115d8 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -1,9 +1,9 @@ /* Find the maximum element of an array of numbers -In this kata, you will need to implement a function that find the largest numerical element of an array. +In this kata, you will need to implement a function that finds the largest numerical element of an array. E.g. max([30, 50, 10, 40]), target output: 50 -E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) +E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (max ignores any non-numerical elements) You should implement this function in max.js, and add tests for it in this file. @@ -16,28 +16,59 @@ 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([44])).toBe(44); +}); + // 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 number overall", () => { + expect(findMax([-10, 0, 5, 30, -40])).toBe(30); +}); + // 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 only negative numbers, returns the number closest to zero", () => { + expect(findMax([-20, -4, -43, -33, -7])).toBe(-4); +}); + // 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 number", () => { + expect(findMax([2.3, 4.4, 5.5, 1.2, 6.6])).toBe(6.6); +}); + // 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 number and ignores non-numeric values", () => { + expect(findMax(["Spongebob", 2, "Squarepants", 5, 8, "patrick", 20])).toBe( + 20 + ); +}); + // 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-numeric values, returns -Infinity", () => { + expect(findMax(["Spongebob", "Squarepants", null, undefined])).toBe( + -Infinity + ); +}); From 1a8b7cfd70b411e2b3863be7295ed9f5f2f4e9c1 Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Sun, 5 Apr 2026 15:58:38 +0200 Subject: [PATCH 6/7] Implement dedupe and add tests --- Sprint-1/implement/dedupe.js | 14 +++++++++++++- Sprint-1/implement/dedupe.test.js | 22 +++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..37b603655 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,13 @@ -function dedupe() {} +function dedupe(list) { + let result = []; + + for (let i = 0; i < list.length; i++) { + if (!result.includes(list[i])) { + result.push(list[i]); + } + } + + return result; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..8afb7bddf 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,32 @@ 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]); +}); // 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 + +// Duplicated strings. +test("given an array of duplicate strings, it removes duplicates and keeps the first occurrence", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); +}); + +// Duplicated numbers. +test("given an array of duplicate numbers, it removes duplicates and keeps the first occurrence", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); +}); + +// Mixed vanlues of numbers and strings. +test("given a mixed array with duplicates, it preserves the first occurrence of each element", () => { + expect(dedupe([1, "a", 1, "a", 2, "b", 2])).toEqual([1, "a", 2, "b"]); +}); From a4523d35d9a5b50809c32da885de25bfb3f8be1e Mon Sep 17 00:00:00 2001 From: Ben Solar Date: Sun, 5 Apr 2026 15:58:46 +0200 Subject: [PATCH 7/7] Refactor includes to use for...of --- Sprint-1/refactor/includes.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..067c2bfba 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,8 @@ // 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) { + // const element = list[index]; => We don't need this line of code when using the for...of loop. if (element === target) { return true; }