From cae4a14e5e17e7f0b6c8587416272c449338b486 Mon Sep 17 00:00:00 2001 From: vmoratti Date: Fri, 24 Jul 2026 16:19:42 +0100 Subject: [PATCH 01/16] rewrite function --- Sprint-1/fix/median.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..a168bc604 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,10 +5,36 @@ // 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). +// Fix this implementation +// Start by running the tests for this function +// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory + +// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) +// or 'list' has mixed values (the function is expected to sort only numbers). +// Fix this implementation +// Start by running the tests for this function +// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory + +// Hint: Please consider scenarios when 'list' 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); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (!Array.isArray(list) || list.length < 2) { + return null; + } + const filteredList = list.filter((value) => typeof value === "number"); + if (filteredList.length < 2) { + return null; + } + const sortedList = filteredList.sort((a, b) => a - b); + const evenLength = sortedList.length % 2 === 0; + const middleIndex = Math.floor(sortedList.length / 2); + if (evenLength) { + const evenListCalc = + (sortedList[middleIndex] + sortedList[middleIndex - 1]) / 2; + return evenListCalc; + } else { + return sortedList[middleIndex]; + } } module.exports = calculateMedian; From f9211a3df2f4e9d46fedef4b7771997374936958 Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sat, 25 Jul 2026 08:30:22 +0100 Subject: [PATCH 02/16] write function and create two tests --- Sprint-1/implement/max.js | 8 ++++++++ Sprint-1/implement/max.test.js | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..fdc1d8938 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,12 @@ function findMax(elements) { + if (elements.length < 1) { + return "-Infinity"; + } else { + const filteredList = elements.filter((value) => typeof value === "number"); + return Math.max( ...filteredList)} + } module.exports = findMax; + +console.log(findMax([30, 50, 10, 40])); // 50 diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..152aff90f 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,11 +16,20 @@ 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([])).toEqual("-Infinity"); +}); // Given an array with one number // When passed to the max function // Then it should return that number +describe("findMax", () => { + [ + { input: [3], expected: 3 }, + ].forEach(({ input, expected }) => + it(`returns the only number in the array for [${input}]`, () => expect(findMax (input)).toEqual(expected)) + ); +}); // Given an array with both positive and negative numbers // When passed to the max function From 183be2371fc6e649833caf80b76c4f60894438e8 Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sat, 25 Jul 2026 15:42:45 +0100 Subject: [PATCH 03/16] change function, write some tests --- Sprint-1/implement/max.js | 4 ++-- Sprint-1/implement/max.test.js | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index fdc1d8938..f6110284b 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,6 +1,6 @@ function findMax(elements) { if (elements.length < 1) { - return "-Infinity"; + return -Infinity; } else { const filteredList = elements.filter((value) => typeof value === "number"); return Math.max( ...filteredList)} @@ -9,4 +9,4 @@ function findMax(elements) { module.exports = findMax; -console.log(findMax([30, 50, 10, 40])); // 50 +//console.log(findMax([30, 50, 10, 40])); // 50 diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 152aff90f..3b9f24716 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,30 @@ 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("given an empty array, returns -Infinity", () => { - expect(findMax([])).toEqual("-Infinity"); -}); // Given an array with one number // When passed to the max function // Then it should return that number describe("findMax", () => { - [ - { input: [3], expected: 3 }, - ].forEach(({ input, expected }) => - it(`returns the only number in the array for [${input}]`, () => expect(findMax (input)).toEqual(expected)) + [{ input: [3], expected: 3 }].forEach(({ input, expected }) => + it(`returns the only number in the array for [${input}]`, () => + expect(findMax(input)).toEqual(expected)) ); }); // 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([1, -2, 3, -4, 5])).toEqual(5); +}); // 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 closest to zero", () => { + expect(findMax([-1, -2, -3, -4])).toEqual(-1); +}); // Given an array with decimal numbers // When passed to the max function From 8e59e19d165d07476b75a1022132b1e7c28ac779 Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sat, 25 Jul 2026 15:47:59 +0100 Subject: [PATCH 04/16] write test for decimal numbers --- 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 3b9f24716..06fa74af4 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -44,6 +44,9 @@ test("given an array with just negative numbers, returns closest 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 largest decimal number", () => { + expect(findMax([1.5, 1.6, 1.7, 1.8])).toEqual(1.8); +}) // Given an array with non-number values // When passed to the max function From f9080a39071c1592c98155388caf49b3c8a76a0f Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sat, 25 Jul 2026 15:51:49 +0100 Subject: [PATCH 05/16] write test for mixed array of numbers and non-numbers --- Sprint-1/implement/max.test.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 06fa74af4..49e6ba20d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -45,12 +45,21 @@ test("given an array with just negative numbers, returns closest to zero", () => // When passed to the max function // Then it should return the largest decimal number test("given an array with decimal numbers, returns largest decimal number", () => { - expect(findMax([1.5, 1.6, 1.7, 1.8])).toEqual(1.8); -}) + expect(findMax([1.5, 1.6, 1.7, 1.8])).toEqual(1.8); + expect(findMax([-1.5, -1.6, -1.7, -1.8])).toEqual(-1.5); +}); // 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 ignores non-numeric values", () => { + expect(findMax([1, 2, "3", null, undefined, 4])).toEqual(4); + expect(findMax(["apple", 1, 2, 3, "banana", 4])).toEqual(4); + expect(findMax([1, "2", 3, "4", 5])).toEqual(5); + expect(findMax([1, "apple", 2, null, 3, undefined, 4])).toEqual(4); + expect(findMax([3, "apple", 1, null, 2, undefined, 4])).toEqual(4); + expect(findMax(["banana", 5, 3, "apple", 1, 4, 2])).toEqual(5); +}); // Given an array with only non-number values // When passed to the max function From 72267a0bef470f9f84bff07756136680b5a1a90f Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sat, 25 Jul 2026 15:55:46 +0100 Subject: [PATCH 06/16] write test for mixed array of non-numbers --- Sprint-1/implement/max.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 49e6ba20d..9fde5254e 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -64,3 +64,8 @@ test("given an array with non-number values, returns the max and ignores non-num // 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(["apple", null, undefined])).toEqual(-Infinity); + expect(findMax([null, undefined])).toEqual(-Infinity); + expect(findMax(["apple", "banana"])).toEqual(-Infinity); +}); \ No newline at end of file From 4f2f012020732d02e4e7954fda67c8359c7031a6 Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sat, 25 Jul 2026 23:00:03 +0100 Subject: [PATCH 07/16] add empty array test --- 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 9fde5254e..96a2b71eb 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,6 +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("given an empty array, returns -Infinity", () => { + expect(findMax([])).toEqual(-Infinity); +}); // Given an array with one number // When passed to the max function From f48b3b96bad8d9f7e5cebe1ef1369d6d04f852ea Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sun, 26 Jul 2026 11:54:20 +0100 Subject: [PATCH 08/16] write sum function --- Sprint-1/implement/sum.js | 5 +++++ Sprint-1/implement/sum.test.js | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..1d319c79f 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,9 @@ function sum(elements) { + let innit = 0; + for (let i = 0; i < elements.length; i++) { + innit += elements[i]; + } +return innit; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..a4b2172b7 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,11 +13,16 @@ 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([])).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 just one number returns that number", () => { + expect(sum([2])).toEqual(2); +}); // Given an array containing negative numbers // When passed to the sum function From 4fe95d91dad505b453fb06c8f5fe50c24eeff09c Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sun, 26 Jul 2026 11:57:30 +0100 Subject: [PATCH 09/16] add tests for negative and decimal numbers --- Sprint-1/implement/sum.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a4b2172b7..924c1ab03 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -27,15 +27,23 @@ test("given an array with just one number returns 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 containing negative numbers, returns the correct total sum", () => { + expect(sum([-1, -2, -3])).toEqual(-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/float numbers, returns the correct total sum", () => { + expect(sum([1.5, 2.5, 3.5])).toEqual(7.5); + expect(sum([-1.5, -2.5, -3.5])).toEqual(-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 + // 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 From 120a27968c945712b5eeb5d8b53327b4b6ecb9c5 Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sun, 26 Jul 2026 12:08:00 +0100 Subject: [PATCH 10/16] write test for mixed array --- Sprint-1/implement/sum.test.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 924c1ab03..772b179e1 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -42,7 +42,11 @@ test("given an array with decimal/float numbers, 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-number values, ignores the non-numerical values and returns the sum of the numerical elements", () => { + expect(sum([1, 2, "3", null, undefined, 4])).toEqual(7); + expect(sum(["apple", 1, 2, 3, "banana", 4])).toEqual(10); + expect(sum([1, "2", 3, "4", 5])).toEqual(9); +}); // Given an array with only non-number values // When passed to the sum function From a02f092ab0203aae18ca1e88bf348f197684203a Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sun, 26 Jul 2026 12:09:01 +0100 Subject: [PATCH 11/16] add validation to the function --- Sprint-1/implement/sum.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 1d319c79f..abbbf78f0 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,9 +1,10 @@ function sum(elements) { let innit = 0; - for (let i = 0; i < elements.length; i++) { - innit += elements[i]; + const filteredList = elements.filter((value) => typeof value === "number"); + for (let i = 0; i < filteredList.length; i++) { + innit += filteredList[i]; } -return innit; +return innit; } module.exports = sum; From a22313c558b9c9e97b1f8da2dc1f7453bb7b7682 Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sun, 26 Jul 2026 12:19:31 +0100 Subject: [PATCH 12/16] add test with non-number array --- 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 772b179e1..829462dfb 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -51,3 +51,6 @@ test("given an array containing non-number values, ignores the non-numerical val // 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(["apple", "banana", null, undefined])).toEqual(0); +}); From dc82a684cf46b11f49f5aebbdd99dca63b88e9ae Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sun, 26 Jul 2026 12:24:50 +0100 Subject: [PATCH 13/16] add tests for an empty, and array wwith duplicates --- Sprint-1/implement/dedupe.test.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..5b0514f0f 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]) 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", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); +}); // Given an array of strings or numbers // When passed to the dedupe function From 4522a378c79981a0b3ddf2e16a157037235062a9 Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sun, 26 Jul 2026 13:36:09 +0100 Subject: [PATCH 14/16] write function --- Sprint-1/implement/dedupe.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..80a42daaa 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(arr) { + let newArr = []; + for (let i = 0; i < arr.length; i++) { + if (!newArr.includes(arr[i])) { + newArr.push(arr[i]); + } + } + return newArr; +} + +console.log(dedupe([1,2,2,2,4])) +module.exports = dedupe; From c22b7f9f9a520bc85144d57052a858a78c2ac447 Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sun, 26 Jul 2026 13:40:40 +0100 Subject: [PATCH 15/16] add test with duplicates --- Sprint-1/implement/dedupe.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 5b0514f0f..3a1813b56 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -31,3 +31,7 @@ test("given an array with no duplicates, it returns a copy of the original array // 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 or numbers, it returns a new array with no duplicates removed", () => { + 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]); +}); \ No newline at end of file From 8f921db5e0fff7cc701429e8d860d7f03c0d51b9 Mon Sep 17 00:00:00 2001 From: vmoratti Date: Sun, 26 Jul 2026 17:08:09 +0100 Subject: [PATCH 16/16] refactor includes function --- Sprint-1/refactor/includes.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..37386436a 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,9 +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]; - if (element === target) { + for (let i of list) { + if (i === target) { return true; } }