From 7d07f44aa4f5374d581a4e836fe63a2699eebb9f Mon Sep 17 00:00:00 2001 From: KK Tech Date: Tue, 17 Mar 2026 12:45:07 +0000 Subject: [PATCH 1/5] Completed Sprint 1/ Fix --- Sprint-1/fix/median.js | 21 ++++++++++++++++++--- Sprint-1/fix/median.test.js | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..deb77d620 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,24 @@ // 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 numbersOnly = list.filter(item => typeof item === 'number'); + const sorted = [...numbersOnly].sort((a, b) => a - b); + + if (sorted.length === 0) { + return null; + } else if (sorted.length % 2 === 0) { + const mid1 = sorted.length / 2 - 1; + const mid2 = sorted.length / 2; + const median = (sorted[mid1] + sorted[mid2]) / 2; + return median; + } else { + const middleIndex = Math.floor(sorted.length / 2); + const median = sorted[middleIndex]; + return median; + } } module.exports = calculateMedian; diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..b4e76e14a 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -22,7 +22,7 @@ describe("calculateMedian", () => { { input: [4, 2, 1, 3], expected: 2.5 }, { input: [6, 1, 5, 3, 2, 4], expected: 3.5 }, { input: [110, 20, 0], expected: 20 }, - { input: [6, -2, 2, 12, 14], expected: 6 }, + { input: [6, -2, 2, 12, 14], expected: 6}, ].forEach(({ input, expected }) => it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) ); From ae69f89201260667b1ca8c6a3599e16d693f820b Mon Sep 17 00:00:00 2001 From: KK Tech Date: Tue, 17 Mar 2026 22:21:37 +0000 Subject: [PATCH 2/5] completed the rest of the tasks for Sprint 1 --- Sprint-1/implement/dedupe.js | 14 +++++++++++++- Sprint-1/implement/max.js | 10 +++++++++- Sprint-1/implement/sum.js | 12 +++++++++++- Sprint-1/refactor/includes.js | 9 ++++----- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..f09f9b449 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,13 @@ -function dedupe() {} +function dedupe(array) { + let seen = new Set(); + let result = []; + for (let item of array) { + if (!seen.has(item)) { + seen.add(item); + result.push(item); + } + } + return result; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..16d73b220 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,12 @@ -function findMax(elements) { +function findMax(elements){ + let max = -Infinity; + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number") + if (elements[i] > max) { + max = elements[i]; + } + } + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..545a68277 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,14 @@ -function sum(elements) { +function sum(elements) +{ let sum = 0; + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number"){ + sum += elements[i]; + } + } + + return sum; } + + module.exports = sum; diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..de371593c 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,12 +1,11 @@ // 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; + for (const element of list) { + if (element === target) { + return true; + } } - } return false; } From 1cb5870489ce74a7b22843e50b51dca9034eb648 Mon Sep 17 00:00:00 2001 From: KK Tech Date: Fri, 20 Mar 2026 21:46:55 +0000 Subject: [PATCH 3/5] i have fixed issues following the feedback instructions --- Sprint-1/fix/median.js | 2 +- Sprint-1/fix/median.test.js | 24 ++++++++++++++++++------ Sprint-1/implement/dedupe.test.js | 1 + Sprint-1/implement/max.test.js | 1 + Sprint-1/implement/sum.js | 17 +++++++---------- Sprint-1/implement/sum.test.js | 1 + Sprint-1/refactor/includes.js | 8 ++++---- 7 files changed, 33 insertions(+), 21 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index deb77d620..65f3a7637 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -10,7 +10,7 @@ function calculateMedian(list) { return null; } const numbersOnly = list.filter(item => typeof item === 'number'); - const sorted = [...numbersOnly].sort((a, b) => a - b); + const sorted = numbersOnly.sort((a, b) => a - b); if (sorted.length === 0) { return null; diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index b4e76e14a..c262c3776 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -13,7 +13,8 @@ describe("calculateMedian", () => { { input: [1, 2, 3, 4], expected: 2.5 }, { input: [1, 2, 3, 4, 5, 6], expected: 3.5 }, ].forEach(({ input, expected }) => - it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + it(`returns the median for [${input}]`, () => + expect(calculateMedian(input)).toEqual(expected)) ); [ @@ -22,9 +23,10 @@ describe("calculateMedian", () => { { input: [4, 2, 1, 3], expected: 2.5 }, { input: [6, 1, 5, 3, 2, 4], expected: 3.5 }, { input: [110, 20, 0], expected: 20 }, - { input: [6, -2, 2, 12, 14], expected: 6}, + { input: [6, -2, 2, 12, 14], expected: 6 }, ].forEach(({ input, expected }) => - it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + it(`returns the correct median for unsorted array [${input}]`, () => + expect(calculateMedian(input)).toEqual(expected)) ); it("doesn't modify the input array [3, 1, 2]", () => { @@ -33,8 +35,17 @@ describe("calculateMedian", () => { expect(list).toEqual([3, 1, 2]); }); - [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => - it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) + [ + "not an array", + 123, + null, + undefined, + {}, + [], + ["apple", null, undefined], + ].forEach((val) => + it(`returns null for non-numeric array (${val})`, () => + expect(calculateMedian(val)).toBe(null)) ); [ @@ -45,6 +56,7 @@ describe("calculateMedian", () => { { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 }, { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 }, ].forEach(({ input, expected }) => - it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + it(`filters out non-numeric values and calculates the median for [${input}]`, () => + expect(calculateMedian(input)).toEqual(expected)) ); }); diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..fe3ea444d 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -25,3 +25,4 @@ test.todo("given an empty array, it returns an empty array"); // 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 + diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..6a722c007 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -41,3 +41,4 @@ test.todo("given an empty array, returns -Infinity"); // 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 + diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 545a68277..0c69a4acb 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,14 +1,11 @@ -function sum(elements) -{ let sum = 0; - for (let i = 0; i < elements.length; i++) { - if (typeof elements[i] === "number"){ - sum += elements[i]; - } +function sum(elements) { + let sum = 0; + for (let i = 0; i < elements.length; i++) { + if (Number.isFinite(elements[i])) { + sum += elements[i]; } - - return sum; + } + return sum; } - - module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..2f5337de1 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -34,3 +34,4 @@ test.todo("given an empty array, returns 0") // 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 + diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index de371593c..8c9ae2e66 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,11 +1,11 @@ // Refactor the implementation of includes to use a for...of loop function includes(list, target) { - for (const element of list) { - if (element === target) { - return true; - } + for (const element of list) { + if (element === target) { + return true; } + } return false; } From f8bcc9248aa24d4e5acebcdf6be971293a28161c Mon Sep 17 00:00:00 2001 From: KK Tech Date: Fri, 20 Mar 2026 22:18:07 +0000 Subject: [PATCH 4/5] i have fixed little issues --- Sprint-1/fix/median.js | 2 +- Sprint-1/implement/max.js | 30 ++++++++++++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 65f3a7637..78e8ba0d7 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -9,7 +9,7 @@ function calculateMedian(list) { if (!Array.isArray(list)) { return null; } - const numbersOnly = list.filter(item => typeof item === 'number'); + const numbersOnly = list.filter((item) => Number.isFinite(item)); const sorted = numbersOnly.sort((a, b) => a - b); if (sorted.length === 0) { diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 16d73b220..7a1a74421 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,12 +1,22 @@ -function findMax(elements){ - let max = -Infinity; - for (let i = 0; i < elements.length; i++) { - if (typeof elements[i] === "number") - if (elements[i] > max) { - max = elements[i]; - } - } - return max; +function findMax(elements) { + let max = -Infinity; + let foundNumber = false; + + for (let i = 0; i < elements.length; i++) { + if (Number.isFinite(elements[i])) { + foundNumber = true; + + if (elements[i] > max) { + max = elements[i]; + } + } + } + + if (!foundNumber) { + return null; + } + + return max; } -module.exports = findMax; +module.exports = findMax; \ No newline at end of file From 396a9c0ea9dc8ec127b73192db1c52d6cce2fbad Mon Sep 17 00:00:00 2001 From: KK Tech Date: Sat, 21 Mar 2026 12:37:05 +0000 Subject: [PATCH 5/5] I have modified the files and tested them with Jest and got a Pass --- Sprint-1/implement/dedupe.test.js | 11 ++++++++++- Sprint-1/implement/max.test.js | 23 +++++++++++++++++++++-- Sprint-1/implement/sum.test.js | 20 +++++++++++++++++++- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index fe3ea444d..114c15c20 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,22 @@ 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 the same 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 keeps first occurrence", () => { + 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]); +}); diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 6a722c007..ca10bac96 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,29 +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("given an empty array, returns null", () => { + expect(findMax([])).toBe(null); +}); // 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([7])).toBe(7); +}); // 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 positive and negative numbers, returns the largest number", () => { + expect(findMax([-10, 0, 5, -3, 8])).toBe(8); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("given only negative numbers, returns the largest negative number", () => { + expect(findMax([-10, -4, -2, -30])).toBe(-2); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("given decimal numbers, returns the largest decimal number", () => { + expect(findMax([1.2, 3.7, 2.5])).toBe(3.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("ignores non-number values and returns the max", () => { + expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); +}); // 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 only non-number values, returns null", () => { + expect(findMax(["hey", "hi", null, undefined, NaN])).toBe(null); +}); diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 2f5337de1..ab9d532bf 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,25 +13,43 @@ 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([7])).toBe(7); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("given negative numbers, returns the correct total", () => { + expect(sum([-10, -3, -2])).toBe(-15); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given decimal numbers, returns the correct total", () => { + 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 and returns the sum", () => { + 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 only non-number values, returns 0", () => { + expect(sum(["hey", "hi", null, undefined, NaN])).toBe(0); +});