From 6f316bef4e2c6ac648b652667391fdc747a7aa02 Mon Sep 17 00:00:00 2001 From: Hendrine Zeraua Date: Thu, 17 Jul 2025 11:18:21 +0100 Subject: [PATCH 1/6] fix: correct calculateMedian function to handle sorting and non-numeric values --- Sprint-1/fix/median.js | 45 +++++++++++++++++++++++++++++++++++++----- mean.js | 0 mean.test.js | 0 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 mean.js create mode 100644 mean.test.js diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..442967dd1 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); - const median = list.splice(middleIndex, 1)[0]; - return median; -} +// function calculateMedian(list) { +// const middleIndex = Math.floor(list.length / 2); +// const median = list.splice(middleIndex, 1)[0]; +// return median; +// } module.exports = calculateMedian; + + +// I followed the instructions to fix the calculateMedian function: + +// I started by attempting to run the tests in the Sprint-1/fix directory using npm test -- fix. +// After resolving setup issues and running npx jest, I reviewed the test failures. +// I then analysed the original code and realised it: +// - Didn’t sort the array before finding the median +// - Mutated the original list using .splice() +// - Didn’t handle non-numeric values or invalid inputs +// I rewrote the function to: +// - Filter out all non-numeric values (including NaN) +// - Handle edge cases like no numbers in the list or invalid input +// - Sort only the numeric values in ascending order +// - Correctly calculate the median for both even and odd-length arrays +// I avoided modifying the original input list by using the spread operator ([...numbers]) +// I used the test results to confirm that the function now works as expected + +function calculateMedian(list) { + + if (!Array.isArray(list)) return null; + + const numbers = list.filter(n => typeof n === 'number' && !isNaN(n)); + + if (numbers.length === 0) return null; + + const sorted = [...numbers].sort((a, b) => a - b); + const mid = Math.floor(sorted.length / 2); + + if (sorted.length % 2 === 0) { + return (sorted[mid - 1] + sorted[mid]) / 2; + } else { + return sorted[mid]; + } +} \ No newline at end of file diff --git a/mean.js b/mean.js new file mode 100644 index 000000000..e69de29bb diff --git a/mean.test.js b/mean.test.js new file mode 100644 index 000000000..e69de29bb From 66100632f58c32d0b15f45e467db2d7aeddbecda Mon Sep 17 00:00:00 2001 From: Hendrine Zeraua Date: Thu, 17 Jul 2025 12:52:37 +0100 Subject: [PATCH 2/6] add explanation for passing test cases in median.test.js --- Sprint-1/fix/median.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..7ede096df 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -4,6 +4,14 @@ // passing all the tests... // Fix the implementation of calculateMedian so it passes all tests +// All test cases are passing because: +// The calculateMedian function in median.js was updated to: +// • Filter out non-numeric values +// • Avoid mutating the input array +// • Correctly calculate medians for odd and even-length arrays +// • Handle unsorted and mixed-type inputs gracefully +// This confirms the function behaves as expected across a wide range of scenarios. + const calculateMedian = require("./median.js"); describe("calculateMedian", () => { From 5ef10ca631459453e1c39e6ebd56cfde3326eccf Mon Sep 17 00:00:00 2001 From: Hendrine Zeraua Date: Thu, 17 Jul 2025 14:13:34 +0100 Subject: [PATCH 3/6] implement findMax function and test cases: handles empty arrays, numbers, decimals, and non-numeric values --- Sprint-1/implement/max.js | 32 +++++++++++++++++++++++++++++--- Sprint-1/implement/max.test.js | 27 ++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..673c3f60e 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,30 @@ -function findMax(elements) { -} +// function findMax(elements) { +// } + +// module.exports = findMax; + -module.exports = findMax; +// In my implementation of findMax: +// I first checked if the input is an array. If it isn’t, I return -Infinity as a safe fallback (e.g. for non-array inputs). +// Then, I filtered out all non-numeric values using .filter() and typeof el === "number", so that only valid numbers are considered. +// If there are no numeric values, I return -Infinity (which is consistent with how Math.max() behaves on an empty array). +// Finally, I used Math.max(...numbersOnly) with the spread operator to return the highest value from the filtered list. +// This approach ensures that the function handles: +// Empty arrays +// Arrays with all non-numbers +// Mixed arrays (e.g., numbers with strings or null) +// Positive, negative, and decimal numbers +// Without mutating the original array +// I also confirmed the function passes all tests, including edge cases. + +function findMax(elements) { + if (!Array.isArray(elements)) return -Infinity; + + const numbersOnly = elements.filter((el) => typeof el === "number"); + + if (numbersOnly.length === 0) return -Infinity; + + return Math.max(...numbersOnly); + } + + module.exports = findMax; \ No newline at end of file diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..935257bc4 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,53 @@ 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 both positive and negative numbers, returns the largest", () => { + expect(findMax([-10, 0, 20, -5, 15])).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 just negative numbers, returns the closest one to zero", () => { + expect(findMax([-100, -1, -50])).toBe(-1); + }); // 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.1, 2.5, 3.9, 3.8])).toBe(3.9); + }); // 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, ignores them and returns max", () => { + expect(findMax(["a", 1, "b", 3, null, 2])).toBe(3); + }); + // 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, {}, []])).toBe(-Infinity); + }); + From 5d24c4ef33df4e1b278944dde55f8c9eec92705f Mon Sep 17 00:00:00 2001 From: Hendrine Zeraua Date: Thu, 17 Jul 2025 15:33:55 +0100 Subject: [PATCH 4/6] =?UTF-8?q?implement=20sum=20function=20=E2=80=93=20fi?= =?UTF-8?q?lters=20non-numbers,=20handles=20empty,=20negative,=20decimal?= =?UTF-8?q?=20values.=20I=20understand=20how=20it=20works=20and=20tested?= =?UTF-8?q?=20it=20with=20different=20cases.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sprint-1/implement/sum.js | 27 +++++++++++++++++++++++++-- Sprint-1/implement/sum.test.js | 24 ++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..5463ff069 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,27 @@ +// function sum(elements) { +// } + +// module.exports = sum; + +/* + I implemented a function called sum that takes an array and returns + the total of all numeric values. I made sure to filter out any non-numeric + elements like strings or nulls. + + I understand that: + - If the array is empty or has no numbers, it should return 0. + - It needs to handle negative numbers and decimal numbers correctly. + - Non-number values should be ignored to prevent errors or incorrect totals. +*/ + function sum(elements) { -} + if (!Array.isArray(elements)) return 0; + + return elements + .filter((el) => typeof el === "number") + .reduce((total, num) => total + num, 0); + } + + module.exports = sum; + -module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..309cb5809 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,44 @@ 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 an array with 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("given an array with negative numbers, returns correct sum", () => { + expect(sum([-5, -10, -15])).toBe(-30); + }); // 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 correct sum", () => { + expect(sum([1.5, 2.5, 3.25])).toBeCloseTo(7.25); + }); // 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 with non-numeric values, returns sum of numbers only", () => { + expect(sum(["apple", 10, null, 20, "banana", 30])).toBe(60); + }); + // 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(["cat", null, undefined, "dog"])).toBe(0); + }); + From e7a2afad9c6b2bb845ce929edb088b08716a762d Mon Sep 17 00:00:00 2001 From: Hendrine Zeraua Date: Thu, 17 Jul 2025 16:31:04 +0100 Subject: [PATCH 5/6] implement dedupe function and test cases: handles duplicates, order preservation, and empty inputs --- Sprint-1/implement/dedupe.js | 23 ++++++++++++++++++++++- Sprint-1/implement/dedupe.test.js | 17 ++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..3f8f2ec56 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,22 @@ -function dedupe() {} +//I used a Set to track seen values while looping to preserve order. +// It passes all test cases including edge cases like empty arrays and type-sensitive inputs. + +function dedupe(array) { + if (!Array.isArray(array)) return []; + + const seen = new Set(); + const result = []; + + for (const item of array) { + if (!seen.has(item)) { + seen.add(item); + result.push(item); + } + } + + return result; + } + + module.exports = dedupe; + + diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..248954735 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,27 @@ 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("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 2: No duplicates +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 +test("removes duplicates and preserves first occurrence (strings)", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + }); + + test("removes duplicates and preserves first occurrence (numbers)", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + }); + From 82564fa3c29320be68ed55b2a3bbe4ba288c3afb Mon Sep 17 00:00:00 2001 From: Hendrine Zeraua Date: Thu, 17 Jul 2025 18:35:12 +0100 Subject: [PATCH 6/6] refactor includes.js to use for...of loop, tests still passing --- Sprint-1/refactor/includes.js | 28 ++++++++++++++++++++++++++-- Sprint-1/refactor/includes.test.js | 3 ++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..b339bdd3b 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,32 @@ // 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) { +// return true; +// } +// } +// return false; +// } + +// module.exports = includes; + + +/* + +I refactored the includes function by changing the for loop to a for...of loop. +Originally, the function used a standard for loop with an index to go through the array. +I replaced that with a for...of loop, which lets me loop directly over each element in the list. +The logic stayed the same — I checked if the current element matches the target, and returned true if it did. +If the loop finishes without finding a match, the function returns false. +After making this change, I ran all the tests in includes.test.js, and they all passed. +This shows the function still works correctly after the refactor. + +*/ + 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; } diff --git a/Sprint-1/refactor/includes.test.js b/Sprint-1/refactor/includes.test.js index 812158470..671e4dfd5 100644 --- a/Sprint-1/refactor/includes.test.js +++ b/Sprint-1/refactor/includes.test.js @@ -1,5 +1,6 @@ // Refactored version of includes should still pass the tests below: - +// All tests passed after refactoring includes.js to use a for...of loop. +// These tests confirm the function still works correctly. const includes = require("./includes.js"); test("returns true when target is in array", () => {