From 53784daf1319c056eeed24a334fcd46e05d01de5 Mon Sep 17 00:00:00 2001 From: marthak1 Date: Thu, 19 Mar 2026 00:27:08 +0000 Subject: [PATCH 1/6] implement calculate median function --- Sprint-1/fix/median.js | 25 ++++++++++++++++++++++--- Sprint-1/implement/max.js | 1 + Sprint-1/implement/max.test.js | 4 +++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..13ca3cd83 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,28 @@ // 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; + // Step 1: must be an array + if (!Array.isArray(list)) return null; + + // Step 2: filter numeric values + const numericValues = list.filter( + (item) => typeof item === "number" && !Number.isNaN(item) + ); + + // Step 3: must have at least one number + if (numericValues.length === 0) return null; + + // Step 4: sort safely + const sortedList = numericValues.slice().sort((a, b) => a - b); + + const middleIndex = Math.floor(sortedList.length / 2); + + // Step 5: median + if (sortedList.length % 2 !== 0) { + return sortedList[middleIndex]; + } + + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..8fbc5903c 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,5 @@ function findMax(elements) { + return Math.max(...elements) } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..f166364d9 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,7 +16,9 @@ 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 -Infinity", () =>{ + +}); // Given an array with one number // When passed to the max function From 605551b543c6db14e9ad99e74568f8435bbf2d6f Mon Sep 17 00:00:00 2001 From: marthak1 Date: Thu, 19 Mar 2026 18:29:13 +0000 Subject: [PATCH 2/6] implement findMax function with test cases --- Sprint-1/implement/max.js | 8 +++++++- Sprint-1/implement/max.test.js | 36 ++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 8fbc5903c..848c815fb 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,5 +1,11 @@ function findMax(elements) { - return Math.max(...elements) + if (elements.length === 0 && elements !== "number") return -Infinity; + + return Math.max( + ...elements.filter( + (item) => typeof item === "number" && !Number.isNaN(item) + ) + ); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index f166364d9..9e41e0118 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,30 +16,62 @@ 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("given an empty array, returns -Infinity", () =>{ - +it("given an empty array, returns -Infinity", () => { + const input = [] + const result = findMax(input) + expect(result).toBe(-Infinity); }); // Given an array with one number // When passed to the max function // Then it should return that number +it('given an array with one number, its should return that number', () => { + const input = [5] + const result = findMax(input) + expect(result).toBe(5); +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +it('given an array with both positive and negative, it should return largest overall', () => { + const input = [5, -5, -10, -1, 6, 8, 10] + const result = findMax(input) + expect(result).toBe(10); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +it('given an array with just negative numbers, it should return the closest one to zero', () => { + const input = [-10, -5, -2, -8]; + const result = findMax(input); + expect(result).toBe(-2); +}) // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +it("given an array with just decimal numbers, it should return the largest decimal number", () => { + const input = [1.0, 2.5, 2.2, 5.8]; + const result = findMax(input); + expect(result).toBe(5.8); +}); // Given an array with non-number values // When passed to the max function // Then it should return the max and ignore non-numeric values +it("given an array with non-number value, it should return the max and ignore non-numeric value", () => { + const input = [4, 5, 'arr', 2, []," ", 15, 8]; + const result = findMax(input); + expect(result).toBe(15); +}); // 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 +it("given an array with only non-number value, it should return the least surprising value given how it behaves for all other inputs", () => { + const input = [true, {}, "arr", null, [], " ", NaN]; + const result = findMax(input); + expect(result).toBe(-Infinity); +}); \ No newline at end of file From ecc365df7b528f09650d84301a46a4435113e355 Mon Sep 17 00:00:00 2001 From: marthak1 Date: Thu, 19 Mar 2026 18:40:34 +0000 Subject: [PATCH 3/6] update findMax test cases with jest function naming --- Sprint-1/implement/max.test.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 9e41e0118..d0af145a1 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,7 +16,7 @@ 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. -it("given an empty array, returns -Infinity", () => { +test("given an empty array, returns -Infinity", () => { const input = [] const result = findMax(input) expect(result).toBe(-Infinity); @@ -25,7 +25,7 @@ it("given an empty array, returns -Infinity", () => { // Given an array with one number // When passed to the max function // Then it should return that number -it('given an array with one number, its should return that number', () => { +test('given an array with one number, its should return that number', () => { const input = [5] const result = findMax(input) expect(result).toBe(5); @@ -34,7 +34,7 @@ it('given an array with one number, its 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 -it('given an array with both positive and negative, it should return largest overall', () => { +test('given an array with both positive and negative, it should return largest overall', () => { const input = [5, -5, -10, -1, 6, 8, 10] const result = findMax(input) expect(result).toBe(10); @@ -43,7 +43,7 @@ it('given an array with both positive and negative, it should return largest ove // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero -it('given an array with just negative numbers, it should return the closest one to zero', () => { +test('given an array with just negative numbers, it should return the closest one to zero', () => { const input = [-10, -5, -2, -8]; const result = findMax(input); expect(result).toBe(-2); @@ -52,7 +52,7 @@ it('given an array with just negative numbers, it should return the closest one // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number -it("given an array with just decimal numbers, it should return the largest decimal number", () => { +test("given an array with just decimal numbers, it should return the largest decimal number", () => { const input = [1.0, 2.5, 2.2, 5.8]; const result = findMax(input); expect(result).toBe(5.8); @@ -61,7 +61,7 @@ it("given an array with just decimal numbers, it should return the largest decim // Given an array with non-number values // When passed to the max function // Then it should return the max and ignore non-numeric values -it("given an array with non-number value, it should return the max and ignore non-numeric value", () => { +test("given an array with non-number value, it should return the max and ignore non-numeric value", () => { const input = [4, 5, 'arr', 2, []," ", 15, 8]; const result = findMax(input); expect(result).toBe(15); @@ -70,7 +70,7 @@ it("given an array with non-number value, it should return the max and ignore no // 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 -it("given an array with only non-number value, it should return the least surprising value given how it behaves for all other inputs", () => { +test("given an array with only non-number value, it should return the least surprising value given how it behaves for all other inputs", () => { const input = [true, {}, "arr", null, [], " ", NaN]; const result = findMax(input); expect(result).toBe(-Infinity); From 6af6d5945b46125b3f37cd30d58ec1259dbafc66 Mon Sep 17 00:00:00 2001 From: marthak1 Date: Thu, 19 Mar 2026 23:09:00 +0000 Subject: [PATCH 4/6] implement sum function ans created tested cases --- Sprint-1/implement/max.js | 3 +-- Sprint-1/implement/sum.js | 4 ++++ Sprint-1/implement/sum.test.js | 31 ++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 848c815fb..5088fb0b7 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,6 +1,5 @@ function findMax(elements) { - if (elements.length === 0 && elements !== "number") return -Infinity; - + if (!Array.isArray(elements) || elements.length === 0) return -Infinity; return Math.max( ...elements.filter( (item) => typeof item === "number" && !Number.isNaN(item) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..542e623ea 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,8 @@ function sum(elements) { + if (!Array.isArray(elements) || elements.length === 0) return 0; + return elements + .filter((item) => typeof item === "number" && !Number.isNaN(item)) + .reduce((acc, n) => acc + n, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..317eaebe6 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,53 @@ 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", () => { + const input = []; + const result = sum(input); + expect(result).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 just one number, it should return that number", () => { + const input = [5]; + const result = sum(input); + expect(result).toBe(5); +}); // 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 containing negative numbers, it should return the correct total sum", () => { + const input = [5, -6, 10, -12]; + const result = sum(input); + expect(result).toBe(-3); +}); // 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/float numbers, it should return the correct total sum", () => { + const input = [5, 6.6, 10, 5.12]; + const result = sum(input); + expect(result).toBe(26.720000000000002); +}); // 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 containing non-number values, it should ignore the numerical values and return sum of numeric element", () => { + const input = [4, 5, "arr", 2, [], " ", 15, 8]; + const result = sum(input); + expect(result).toBe(34); +}); // 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, it should return least surprising value given how it behaves for all other inputs", () => { + const input = [true, {}, "arr", null, [], " ", NaN]; + const result = sum(input); + expect(result).toBe(0); +}); From 495006416dbdd1d54c57540449f066d6a26232ca Mon Sep 17 00:00:00 2001 From: marthak1 Date: Fri, 20 Mar 2026 00:01:24 +0000 Subject: [PATCH 5/6] implement dedupe function and wrote test cases --- Sprint-1/implement/dedupe.js | 6 +++++- Sprint-1/implement/dedupe.test.js | 23 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..08648ac3a 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(elements) { + if (elements.length === 0) return []; + return elements.filter((item, index) => elements.indexOf(item) === index); +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..363636a58 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,31 @@ 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", () => { + const input = []; + const result = dedupe(input); + expect(result).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 return a copy of the original array", () => { + const input = [1, 2, 3, 4, 5]; + const result = dedupe(input); + expect(result).toEqual([1, 2, 3, 4, 5]); +}); // 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 +// Then it should remove the duplicate values, preserving the first occurrence of each element +test("given an array with strings, it removes the duplicate, preserving first occurrence of each element", () => { + const input = ["a", "a", "a", "b", "b", "c"]; + const result = dedupe(input); + expect(result).toEqual(["a", "b", "c"]); +}); +test("given an array with numbers it removes the duplicate, preserving first occurrence of each element", () => { + const input = [5, 1, 1, 2, 3, 2, 5, 8]; + const result = dedupe(input); + expect(result).toEqual([5, 1, 2, 3, 8]); +}); \ No newline at end of file From 70627a031bafd0f5a7589b522e256d2b80a13967 Mon Sep 17 00:00:00 2001 From: marthak1 Date: Fri, 20 Mar 2026 09:46:35 +0000 Subject: [PATCH 6/6] refactor includes function to use for ...of loop --- Sprint-1/refactor/includes.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..ecc97ff3d 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; } @@ -11,3 +10,10 @@ function includes(list, target) { } module.exports = includes; + +// const array = ["a", "b", "c"]; + +// for (const element of array) { +// console.log(element); +// } for (variable of iterable) statement +