From 3b68660b094cb0ae19721525463255060808ceb1 Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Fri, 18 Jul 2025 23:16:43 +0100 Subject: [PATCH 01/13] update: fix code returns null for non-numeric array --- Sprint-1/fix/median.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..89866977c 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -8,6 +8,7 @@ function calculateMedian(list) { const middleIndex = Math.floor(list.length / 2); const median = list.splice(middleIndex, 1)[0]; + if (!Array.isArray(list)) return null; return median; } From 552d6b11c1a4af257bb848cadd4a47a760abb5f9 Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Fri, 18 Jul 2025 23:30:41 +0100 Subject: [PATCH 02/13] update: fix code filter to keep only numeric values and function for If no valid numbers, return null --- Sprint-1/fix/median.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 89866977c..3f1b0c9e7 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -8,7 +8,11 @@ function calculateMedian(list) { const middleIndex = Math.floor(list.length / 2); const median = list.splice(middleIndex, 1)[0]; + if (!Array.isArray(list)) return null; + const nums = list.filter(val => typeof val === 'number' && !isNaN(val));//Filter to keep only numeric values + + if (nums.length === 0) return null; // If no valid numbers, return null return median; } From 826e24ba800b0ae621398e09aed230547299f5a3 Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Fri, 18 Jul 2025 23:36:45 +0100 Subject: [PATCH 03/13] update: fix code for odd median by sort the array numerically and return the middle plus --- Sprint-1/fix/median.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 3f1b0c9e7..de975fe05 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -13,6 +13,14 @@ function calculateMedian(list) { const nums = list.filter(val => typeof val === 'number' && !isNaN(val));//Filter to keep only numeric values if (nums.length === 0) return null; // If no valid numbers, return null + + // Clone and sort the array numerically + const sorted = [...nums].sort((a, b) => a - b); + const mid = Math.floor(sorted.length / 2); + + // If odd, return the middle + if (sorted.length % 2 !== 0) { + return sorted[mid]; return median; } From b2b889c6feb71182a531ae375aeea2f14da63742 Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Sat, 19 Jul 2025 00:02:53 +0100 Subject: [PATCH 04/13] update: fixe code by including Return median based on even or odd length --- Sprint-1/fix/median.js | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index de975fe05..7097cf84b 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -1,27 +1,21 @@ -// 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]; - + // Check if input is a valid array if (!Array.isArray(list)) return null; - const nums = list.filter(val => typeof val === 'number' && !isNaN(val));//Filter to keep only numeric values - - if (nums.length === 0) return null; // If no valid numbers, return null - // Clone and sort the array numerically + // Filter out only numeric values + const nums = list.filter(val => typeof val === 'number' && !isNaN(val)); + if (nums.length === 0) return null; + + // Clone and sort the array numerically const sorted = [...nums].sort((a, b) => a - b); const mid = Math.floor(sorted.length / 2); - // If odd, return the middle + // Return median based on even or odd length if (sorted.length % 2 !== 0) { return sorted[mid]; - return median; + } else { + return (sorted[mid - 1] + sorted[mid]) / 2; + } } module.exports = calculateMedian; From a226f7db34b649151895c6b6a8bc5d13dcf90be4 Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Sat, 19 Jul 2025 00:35:34 +0100 Subject: [PATCH 05/13] update: write duplicate function code --- Sprint-1/implement/dedupe.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..bfedf4ed4 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,17 @@ -function dedupe() {} +function dedupe(arr) { + if (!Array.isArray(arr)) return []; + + const seen = new Set(); + const result = []; + + for (const item of arr) { + if (!seen.has(item)) { + seen.add(item); + result.push(item); + } + } + + return result; +} + +module.exports = dedupe; From 038d53774419afad2fbc65b4564f86a17ca5d870 Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Sat, 19 Jul 2025 00:37:41 +0100 Subject: [PATCH 06/13] update: fix code test function for returns an empty array when given an empty array --- Sprint-1/implement/dedupe.test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..b08e9ebad 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,8 +16,10 @@ 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.todo("given an empty array, it returns an empty array"); + test("returns an empty array when given 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 From d2749fc541638bdc331b76d4371e4a9cebf9180d Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Sat, 19 Jul 2025 00:45:38 +0100 Subject: [PATCH 07/13] update: test code for given an array of numbers with no duplicates, it returns a copy of the original array --- Sprint-1/implement/dedupe.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index b08e9ebad..99f5b6119 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -23,6 +23,19 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] // 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 of numbers with no duplicates, it returns a copy of the original array", () => { + const input = [1, 2, 3]; + const result = dedupe(input); + expect(result).toEqual([1, 2, 3]); + expect(result).not.toBe(input); // Ensure it's a new array, not the same reference +}); + +test("given an array of strings with no duplicates, it returns a copy of the original array", () => { + const input = ["a", "b", "c"]; + const result = dedupe(input); + expect(result).toEqual(["a", "b", "c"]); + expect(result).not.toBe(input); +}); // Given an array with strings or numbers // When passed to the dedupe function From 7c3a42dfc07a2023244c91af6152b55a064bca06 Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Sat, 19 Jul 2025 00:47:18 +0100 Subject: [PATCH 08/13] update: fix test code for removes duplicate numbers, preserving the first occurrence --- Sprint-1/implement/dedupe.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 99f5b6119..b4935b8f0 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -40,3 +40,17 @@ test("given an array of strings with no duplicates, it returns a copy of the ori // 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("removes duplicate numbers, preserving the first occurrence", () => { + expect(dedupe([1, 2, 1])).toEqual([1, 2]); + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); +}); + +test("removes duplicate strings, preserving the first occurrence", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + expect(dedupe(["x", "y", "x", "z", "y"])).toEqual(["x", "y", "z"]); +}); + +test("handles mixed types (strings and numbers)", () => { + expect(dedupe(["1", 1, "1", 2, 2, "2"])).toEqual(["1", 1, 2, "2"]); + expect(dedupe([1, "1", 1, "1"])).toEqual([1, "1"]); +}); From 82243a38ce46ccab7867493dd4f82100576c118c Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Sat, 19 Jul 2025 00:58:16 +0100 Subject: [PATCH 09/13] update: fix code for test including cases given --- Sprint-1/implement/max.test.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..6aa7a184a 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,48 @@ 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.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([42])).toBe(42); +}); // 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 positive and negative numbers, returns the largest", () => { + expect(findMax([-10, 5, 20, -3])).toBe(20); +}); // 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 closest to zero", () => { + expect(findMax([-100, -20, -3, -50])).toBe(-3); +}); // 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.2, 3.5, 2.9, 3.6])).toBe(3.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 of valid numbers", () => { + expect(findMax(["a", 10, "b", 20, null, undefined, NaN, 5])).toBe(20); + expect(findMax([false, 3, true, 7, "hello"])).toBe(7); +}); // 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(["a", null, undefined, NaN, {}, [], "b"])).toBe(-Infinity); + expect(findMax([false, true, "zero", "1", {}, []])).toBe(-Infinity); +}); \ No newline at end of file From ef76a1f0cfe723af0d6e694cd2c3df2bd04dc693 Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Sat, 19 Jul 2025 01:04:15 +0100 Subject: [PATCH 10/13] update: fix code for implement a function that find the largest numerical element of an array --- Sprint-1/implement/max.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..65d9662b8 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,11 @@ + + function findMax(elements) { + if (!Array.isArray(elements)) return -Infinity; + + const nums = elements.filter(val => typeof val === 'number' && !isNaN(val)); + + return nums.length > 0 ? Math.max(...nums) : -Infinity; } module.exports = findMax; From c20e286df2ea77b9672c683882b5ff399a5cb571 Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Sat, 19 Jul 2025 01:13:36 +0100 Subject: [PATCH 11/13] update: fix test function code to implement a function that sums the numerical elements of an array --- Sprint-1/implement/sum.test.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..d346480b9 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,39 @@ 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.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 one number, returns that number", () => { + expect(sum([42])).toBe(42); + }); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum - + test("sums array with negative numbers", () => { + expect(sum([-1, -2, 3, 4])).toBe(4); + }); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum + test("sums array with decimals", () => { + expect(sum([1.5, 2.5, 3])).toBe(7); + }); // 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("ignores non-number values", () => { + expect(sum(["hello", 10, null, 20, undefined, 5])).toBe(35); + }); // 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("returns 0 for array with only non-number values", () => { + expect(sum(["hello", null, undefined, {}, [], "42"])).toBe(0); +}); \ No newline at end of file From 0ba1d5f2e86dd735634b81869ba6445698108b19 Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Sat, 19 Jul 2025 01:15:56 +0100 Subject: [PATCH 12/13] update: fix function code to implement a function that sums the numerical elements of an array --- Sprint-1/implement/sum.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..47ba2ea1d 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,9 @@ function sum(elements) { + if (!Array.isArray(elements)) return 0; + + return elements.reduce((acc, val) => { + return typeof val === "number" && !isNaN(val) ? acc + val : acc; + }, 0); } -module.exports = sum; +module.exports = sum; \ No newline at end of file From d7b2afff639ae28c770b85d75766d80c1eb2ae7a Mon Sep 17 00:00:00 2001 From: SuWebOnes Date: Sat, 19 Jul 2025 01:22:39 +0100 Subject: [PATCH 13/13] update: refactor code function to use a for...of loop --- Sprint-1/refactor/includes.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..7010de4e9 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,5 @@ -// 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; }