From ef89cd9249a9f9e0d12f6f98deacf9d7e1759acb Mon Sep 17 00:00:00 2001 From: khalidbih Date: Sat, 21 Mar 2026 00:48:19 +0000 Subject: [PATCH 1/4] Fix calculateMedian to pass all tests --- Sprint-1/fix/median.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..3d10fa610 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,30 @@ // 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)) return null; + + const numbers = []; + for (let i = 0; i < list.length; i++) { + if (typeof list[i] === "number") { + numbers.push(list[i]); + } + } + + if (numbers.length === 0) { + return null; + } + + numbers.sort(function (a, b) { + return a - b; + }); + + const middleIndex = Math.floor(numbers.length / 2); + + if (numbers.length % 2 !== 0) { + return numbers[middleIndex]; + } + + return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; } module.exports = calculateMedian; From de5a96dbca84887633187634038bab6ba0d76b18 Mon Sep 17 00:00:00 2001 From: khalidbih Date: Sat, 21 Mar 2026 14:35:55 +0000 Subject: [PATCH 2/4] Fix dedupe function and add passing tests --- Sprint-1/implement/dedupe.js | 13 +++++++- Sprint-1/implement/dedupe.test.js | 49 +++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..ac002eb62 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(arr) { + if (!Array.isArray(arr)) return []; + const result = []; + for (let i = 0; i < arr.length; i++) { + if (!result.includes(arr[i])) { + result.push(arr[i]); + } + } + return result; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..71f796955 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,5 +1,8 @@ const dedupe = require("./dedupe.js"); -/* + +describe("dedupe", () => { + /* + Dedupe Array 📖 Dedupe means **deduplicate** @@ -11,18 +14,38 @@ E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8] E.g. dedupe([1, 2, 1]) returns [1, 2] */ -// Acceptance Criteria: + // Acceptance Criteria: + + // Given an empty array + // When passed to the dedupe function + // Then it should return an empty array + test("given an empty array, it returns an empty array", () => { + const input = []; + const output = dedupe(input); + expect(output).toEqual([]); + }); + // Given an array with no duplicates + // When passed to the dedupe function + // Then it should return a copy of the original array + test("array with no duplicates returns a copy", () => { + const input = [1, 2, 3]; + const output = dedupe(input); + expect(output).toEqual([1, 2, 3]); + expect(output).not.toBe(input); + }); -// 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"); + // Given an array of strings or numbers + // 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("removes duplicates while preserving first occurrence", () => { + const input1 = ["a", "a", "a", "b", "b", "c"]; + expect(dedupe(input1)).toEqual(["a", "b", "c"]); -// Given an array with no duplicates -// When passed to the dedupe function -// Then it should return a copy of the original array + const input2 = [5, 1, 1, 2, 3, 2, 5, 8]; + expect(dedupe(input2)).toEqual([5, 1, 2, 3, 8]); -// Given an array of strings or numbers -// 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. + const input3 = [1, 2, 1]; + expect(dedupe(input3)).toEqual([1, 2]); + }); +}); From 41dc44b95b68200b83a63189c50b43e257e1ab75 Mon Sep 17 00:00:00 2001 From: khalidbih Date: Sat, 21 Mar 2026 15:00:46 +0000 Subject: [PATCH 3/4] Fix findMax implementation and complete max.test.js for all test cases --- Sprint-1/implement/max.js | 13 ++++++ Sprint-1/implement/max.test.js | 79 +++++++++++++++++++++------------- 2 files changed, 62 insertions(+), 30 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..c763fa248 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,17 @@ function findMax(elements) { + if (!Array.isArray(elements)) return null; + + let maxValue = -Infinity; + + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number" && isFinite(elements[i])) { + if (elements[i] > maxValue) { + maxValue = elements[i]; + } + } + } + + return maxValue; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..6ca4dc1de 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -11,33 +11,52 @@ We have set things up already so that this file can see your function from the o */ 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"); - -// Given an array with one number -// When passed to the max function -// Then it should return that number - -// Given an array with both positive and negative numbers -// When passed to the max function -// Then it should return the largest number overall - -// Given an array with just negative numbers -// When passed to the max function -// Then it should return the closest one to zero - -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number - -// Given an array with non-number values -// When passed to the max function -// Then it should return the max and ignore non-numeric values - -// 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 +describe("findMax", () => { + // 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("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("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("array with positive and negative numbers returns the largest number", () => { + expect(findMax([-10, 5, 3, -2])).toBe(5); + }); + + // Given an array with just negative numbers + // When passed to the max function + // Then it should return the closest one to zero + test("array with only negative numbers returns the closest to zero", () => { + expect(findMax([-10, -3, -7])).toBe(-3); + }); + // Given an array with decimal numbers + // When passed to the max function + // Then it should return the largest decimal number + test("array with decimal numbers returns the largest decimal", () => { + expect(findMax([1.5, 2.7, 2.6, 0.9])).toBe(2.7); + }); + // 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("array with non-number values ignores them and returns max of numbers", () => { + expect(findMax([10, "a", 5, null, 7])).toBe(10); + }); + + // 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("array with only non-number values returns -Infinity", () => { + expect(findMax(["a", null, "b"])).toBe(-Infinity); + }); +}); From 8a8b878acf3bf0777067b50565bc37c789758c1d Mon Sep 17 00:00:00 2001 From: khalidbih Date: Sat, 21 Mar 2026 23:04:05 +0000 Subject: [PATCH 4/4] Implement sum function and add complete test coverage --- Sprint-1/implement/sum.js | 11 ++++++ Sprint-1/implement/sum.test.js | 70 +++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..d17ab2d92 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,15 @@ function sum(elements) { + if (!Array.isArray(elements)) return 0; + + let total = 0; + + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number" && isFinite(elements[i])) { + 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..c7826e2c6 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -8,29 +8,47 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical const sum = require("./sum.js"); -// Acceptance Criteria: - -// Given an empty array -// When passed to the sum function -// Then it should return 0 -test.todo("given an empty array, returns 0") - -// Given an array with just one number -// When passed to the sum function -// Then it should return that number - -// Given an array containing negative numbers -// When passed to the sum function -// Then it should still return the correct total sum - -// Given an array with decimal/float numbers -// When passed to the sum function -// Then it should return 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 - -// 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 +describe("sum", () => { + // Acceptance Criteria: + + // Given an empty array + // When passed to the sum function + // Then it should return 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("array with one number returns that number", () => { + expect(sum([5])).toBe(5); + }); + + // Given an array containing negative numbers + // When passed to the sum function + // Then it should still return the correct total sum + test("array with negative numbers returns correct sum", () => { + expect(sum([-2, -3, 5])).toBe(0); + }); + + // Given an array with decimal/float numbers + // When passed to the sum function + // Then it should return the correct total sum + test("array with decimal numbers returns correct sum", () => { + expect(sum([1.5, 2.5, 1])).toBe(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("ignores non-number values and sums numbers only", () => { + expect(sum([10, "a", 5, null, 7])).toBe(22); + }); + + // 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 when array has only non-number values", () => { + expect(sum(["a", null, "b"])).toBe(0); + }); +});