From 5c5e165e270770b5ad92977c959f65afff382da3 Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Mon, 9 Mar 2026 20:29:04 +0000 Subject: [PATCH 1/6] fix: handle non-array and non-numeric inputs in calculateMedian --- Sprint-1/fix/median.js | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..c593f1811 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,41 @@ // 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; + let newArray = []; + if(!Array.isArray(list)) + { + return null; + } + + for (const item of list) + { + if (typeof item === 'number') + { + newArray.push(item); + } + + } + if (newArray.length === 0) + { + return null; + } + newArray = newArray.sort((a, b) => a-b); + if(newArray.length %2 === 0) + { + const rightIndex = newArray.length/2 + const leftIndex = rightIndex - 1; + const median = (newArray[rightIndex] + newArray[leftIndex])/2; + return median; + + } + else{ + + const middleIndex = Math.floor(newArray.length/2); + return newArray[middleIndex] + } + //const middleIndex = Math.floor(newArray.length / 2); + //const median = list.splice(middleIndex, 1)[0]; + //return newArray[middleIndex]; } module.exports = calculateMedian; From 4700cb12ac3bd3a4795eb6111ed93873366ac786 Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Tue, 10 Mar 2026 18:45:13 +0000 Subject: [PATCH 2/6] Implement dedupe function --- Sprint-1/implement/dedupe.js | 18 +++++++++++++++++- Sprint-1/implement/dedupe.test.js | 13 ++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..b19b591e3 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,17 @@ -function dedupe() {} +function dedupe(list) { + if (list.length=== 0) + { + return []; + } + let result = []; + for (let item of list) + { + if(!result.includes(item)) + { + result.push(item) + } + } + return result; + +} +module.exports = dedupe; \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..04d0edaa6 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -17,7 +17,18 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] // 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", ()=>{ + const list = []; + expect(dedupe(list)).toEqual([]); +}); +test +("array with no duplicate should return copy of original", ()=>{ + let list = [1,2,4,6,9,8]; + expect(dedupe(list)).toEqual([1,2,4,6,9,8]); +}); +test("shoiuld remove the duplicate value and preserving the first occurebce",()=>{ + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5,1,2,3,8]); +}) // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array From 2b3cdc0ad01a6e4b68b4a9a3b7d2187d35d8e92e Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Tue, 10 Mar 2026 20:25:32 +0000 Subject: [PATCH 3/6] Implement findMax function and ignore non-numeric values --- Sprint-1/implement/max.js | 15 +++++++++++++++ Sprint-1/implement/max.test.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..e69260ad5 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,19 @@ function findMax(elements) { + if(elements.length === 0) + { + return -Infinity; + } + let max = -Infinity ; + for (const item of elements) +{ + if (typeof item === 'number') + { + max = Math.max(max , item); + } + + +} + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..afcf84bdc 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,7 +16,34 @@ 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 should return -Infinity", () => { + const list = []; + expect(findMax(list)).toEqual(-Infinity); +}); +test("Array with one number should return that number", () => { + const list =[7]; + expect(findMax(list)).toEqual(7); +}); +test("An array with both positive and negative number should return the largest overall", () =>{ + const list = [-10, 5, 30, -3]; + expect(findMax(list)).toEqual(30); +}); +test("Array with just negative number should return the closest on to zero", () =>{ + const list = [-10, -3, -20,-7,-4] + expect(findMax(list)).toEqual(-3); +}); +test("An array with decimal number should return largest decimal number", ()=>{ + const list = [1.2, 3.5, 2.8] + expect(findMax(list)).toEqual(3.5); +}); +test("An array with non-numeric values should return the max and ignore non-numeric", () =>{ + const list = ['hey', 10, 'hi', 60, 50]; + expect(findMax(list)).toEqual(60); +}); +test("An array with non numeric value should return the least surprising value given how it behaves for all other inputs", ()=> { + const list = ['a', 'b', 'hello']; + expect(findMax(list)).toEqual(-Infinity); +}) // Given an array with one number // When passed to the max function From f59026c5cc6956e22c3443cb948f283b9f547c77 Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Tue, 10 Mar 2026 20:51:53 +0000 Subject: [PATCH 4/6] Implement sum function --- Sprint-1/implement/sum.js | 15 +++++++++++++++ Sprint-1/implement/sum.test.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..7bb139801 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,19 @@ function sum(elements) { + if (elements.length === 0) + { + return 0; + } + let total = 0 ; + for (const item of elements) +{ + if (typeof item === 'number') + { + total = total + item; + } + + +} + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..0598d34ab 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,7 +13,34 @@ 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 should return -Infinity", () => { + const list = []; + expect(sum(list)).toEqual(0); +}); +test("Array with one number should return that number", () => { + const list =[7]; + expect(sum(list)).toEqual(7); +}); +test("An array with both positive and negative number should return the largest overall", () =>{ + const list = [-10, 5, 30, -3]; + expect(sum(list)).toEqual(22); +}); +test("Array with just negative number should return the closest on to zero", () =>{ + const list = [-10, -3, -20,-7,-4] + expect(sum(list)).toEqual(-44); +}); +test("An array with decimal number should return largest decimal number", ()=>{ + const list = [1.2, 3.5, 2.8] + expect(sum(list)).toEqual(7.5); +}); +test("An array with non-numeric values should return the max and ignore non-numeric", () =>{ + const list = ['hey', 10, 'hi', 60, 50]; + expect(sum(list)).toEqual(120); +}); +test("An array with non numeric value should return the least surprising value given how it behaves for all other inputs", ()=> { + const list = ['a', 'b', 'hello']; + expect(sum(list)).toEqual(0); +}) // Given an array with just one number // When passed to the sum function From b473ef0fa3fde08b49048086b8fe53f70f55c273 Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Wed, 11 Mar 2026 10:19:18 +0000 Subject: [PATCH 5/6] Refactor includes to use for...of loop --- Sprint-1/refactor/includes.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8c9ae2e66 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,7 @@ // 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; } From f2f5f60de1882d2237da6794d7cd358fbe517fb8 Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Mon, 16 Mar 2026 11:33:25 +0000 Subject: [PATCH 6/6] fix dedupe indentation and confirm empty array validation --- Sprint-1/implement/dedupe.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index b19b591e3..e15439a83 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -8,7 +8,7 @@ function dedupe(list) { { if(!result.includes(item)) { - result.push(item) + result.push(item); } } return result;