From 735f29114476c04b68d7a8082907e0e98138aef2 Mon Sep 17 00:00:00 2001 From: ofonimeedak Date: Thu, 19 Mar 2026 11:51:11 +0000 Subject: [PATCH 1/5] write and implement function and test case --- Sprint-1/fix/median.js | 20 +++++-- Sprint-1/implement/dedupe.js | 16 +++++- Sprint-1/implement/dedupe.test.js | 45 +++++++++++++--- Sprint-1/implement/max.js | 17 ++++++ Sprint-1/implement/max.test.js | 83 ++++++++++++++++++++++------- Sprint-1/implement/sum.js | 15 ++++++ Sprint-1/implement/sum.test.js | 88 ++++++++++++++++++++++--------- Sprint-1/refactor/includes.js | 3 +- 8 files changed, 230 insertions(+), 57 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..ade39daee 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,23 @@ // 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 (const x of list) { + if (typeof x === "number" && !isNaN(x)) { + numbers.push(Number(x)); + } + } + if (numbers.length === 0) return null; + numbers.sort((a, b) => a - b); + const length = numbers.length; + const middleIndex = Math.floor(length / 2); + if (length % 2 === 0) { + return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; + } else { + return numbers[middleIndex]; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..68feb25be 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,15 @@ -function dedupe() {} +function dedupe(arr) { + +if(arr.length===0) return arr; +const dedupeArray=[] +for(let i=0;i { + [{ input: [], expected: [] }].forEach(({ input, expected }) => + it(`given an empty array, it returns an empty array [${input}]`, () => { + expect(dedupe(input)).toEqual(expected); + }) + ); + // Given an array with no duplicates + // Then it should return a copy of the original array + [ + { input: [1, 2, 3, 4], expected: [1, 2, 3, 4] }, + { + input: ["apples", "banana", "orange"], + expected: ["apples", "banana", "orange"], + }, + { input: [-1, 7, 1], expected: [-1, 7, 1] }, + ].forEach(({ input, expected }) => + it(`should return same input values [${input}] without duplicate`, () => { + expect(dedupe(input)).toEqual(expected); + })); -// Given an array with no duplicates -// When passed to the dedupe function -// Then it should return a copy of the original array + // When passed to the dedupe function + // 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 -// 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 + [ + { input: [1, 2, 5, 5, "a", 5, 10, 10, "a"], expected: [1, 2, 5, "a", 10] }, + { + input: ["apple", "banana", "orange", "apple", "banana", 1, 3, 4, 1], + expected: ["apple", "banana", "orange", 1, 3,4], + }, + ].forEach(({ input, expected }) => + it(`should return deduplicated array for [${input}]`, () => { + expect(dedupe(input)).toEqual(expected); + }) + ); + +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..29cd6fc6f 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,21 @@ function findMax(elements) { + if (!Array.isArray(elements)) return "invalid elements"; + if (elements.length === 0) return Infinity; + const number = []; + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number" && !Number.isNaN(elements[i])) { + number.push(elements[i]); + } + } + if (number.length === 0) return "invalid elements"; + let max = number[0]; + + for (let i = 1; i < number.length; i++) { + if (max < number[i]) { + max = number[i]; + } + } + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..fbe713014 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,73 @@ 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"); +describe("findMax()", () => { + [{ input: [], expected: Infinity }].forEach(({ input, expected }) => + it(`should return ${expected} for empty [${input}]`, () => { + expect(findMax(input)).toEqual(expected); + }) + ); -// Given an array with one number -// When passed to the max function -// Then it should return that number + // 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 + [{ input: [50], expected: 50 }].forEach(({ input, expected }) => + it(`should return ${expected} for array [${input}]`, () => { + expect(findMax(input)).toEqual(expected); + }) + ); -// 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 both positive and negative numbers + // When passed to the max function + // Then it should return the largest number overall -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number + [{ input: [2, 5, 6, -1, 0, 25, -30], expected: 25 }].forEach( + ({ input, expected }) => { + it(`should return ${expected} for positive and negative numbers in the array`, () => { + expect(findMax(input)).toEqual(expected); + }); + } + ); -// 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 just negative numbers + // When passed to the max function + // Then it should return the closest one to zero + [{ input: [-1, -10, -7, -20], expected: -1 }].forEach(({ input, expected }) => + it(`should return negative number nearest to zero`, () => { + expect(findMax(input)).toEqual(expected); + }) + ); -// 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 + // Given an array with decimal numbers + // When passed to the max function + // Then it should return the largest decimal number + + [{ input: [0.5, 0.1, 0.56, 0.8], expected: 0.8 }].forEach( + ({ input, expected }) => + it(`should return the largest decimal number from the array`, () => { + expect(findMax(input)).toEqual(expected); + }) + ); + + // Given an array with non-number values + // When passed to the max function + // Then it should return the max and ignore non-numeric values + [ + { input: ["edak", "ofonime", "", "@", -4, 10, 6, 50, -100], expected: 50 }, + ].forEach(({ input, expected }) => + it(`should return max numerical value from the array`, () => { + expect(findMax(input)).toEqual(expected); + }) + ); + + // 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 + [{ input: ["peter", "", "@", "Hi"], expected: "invalid elements" }].forEach( + ({ input, expected }) => + it(`should return "invalid elements" for non-numeric values`, () => { + expect(findMax(input)).toEqual(expected); + }) + ); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..1389977b8 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,19 @@ function sum(elements) { + if (!Array.isArray(elements)) return "invalid elements"; + if(elements.length===0) return 0; + const number = []; + for (const x of elements) { + if (typeof x === "number" && !Number.isNaN(x)) { + number.push(x); + } + } + if(number.length===0) return "invalid elements"; + let sumOfNum=0; + for(let i=0; i { + // Given an empty array + // When passed to the sum function + // Then it should return 0 + [{ input: [], expected: 0 }].forEach(({ input, expected }) => + it(`should return ${expected} for [${input}]`, () => { + expect(sum(input)).toEqual(expected); + }) + ); + + // Given an array with just one number + // When passed to the sum function + // Then it should return that number + + [{ input: [30], expected: 30 }].forEach(({ input, expected }) => + it(`should return ${expected} for [${input}]`, () => { + expect(sum(input)).toEqual(expected); + }) + ); + + // Given an array containing negative numbers + // When passed to the sum function + // Then it should still return the correct total sum + + [{ input: [-1, -3, -4, -11], expected: -19 }].forEach(({ input, expected }) => + it(`should return ${expected} for [${input}]`, () => { + expect(sum(input)).toEqual(expected); + }) + ); + + // Given an array with decimal/float numbers + // When passed to the sum function + // Then it should return the correct total sum + + [{ input: [0.5, 0.2, 0.11, 0.89, 0.3], expected: 2 }].forEach( + ({ input, expected }) => + it(`should return ${expected} for [${input}]`, () => { + expect(sum(input)).toEqual(expected); + }) + ); + + // 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 + + [ + { input: ["evan", 3, "mike", 20, 6, "", "/", , , 20], expected: 49 }, + ].forEach(({ input, expected }) => + it(`should return ${expected} for [${input}]`, () => { + expect(sum(input)).toEqual(expected); + }) + ); + + // 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 + [ + { input: ["evan", "mike", "", "/", , ,], expected: "invalid elements" }, + ].forEach(({ input, expected }) => + it(`should return ${expected} for [${input}]`, () => { + expect(sum(input)).toEqual(expected); + }) + ); +}); 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 1531393055942c146aca923c1182761fe5ad1205 Mon Sep 17 00:00:00 2001 From: ofonimeedak Date: Sat, 28 Mar 2026 05:27:10 +0000 Subject: [PATCH 2/5] fixed PR review --- Sprint-1/fix/median.js | 7 +------ Sprint-1/implement/dedupe.js | 20 ++++++++------------ Sprint-1/implement/dedupe.test.js | 14 ++++++++++---- Sprint-1/implement/max.js | 4 ++-- Sprint-1/implement/max.test.js | 13 ++++++++----- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index ade39daee..bee1d7889 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -7,13 +7,8 @@ function calculateMedian(list) { if (!Array.isArray(list)) return null; + const numbers = list.filter((e) => typeof e === "number" && !isNaN(e)); - const numbers = []; - for (const x of list) { - if (typeof x === "number" && !isNaN(x)) { - numbers.push(Number(x)); - } - } if (numbers.length === 0) return null; numbers.sort((a, b) => a - b); const length = numbers.length; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 68feb25be..d33bcda89 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,15 +1,11 @@ function dedupe(arr) { - -if(arr.length===0) return arr; -const dedupeArray=[] -for(let i=0;i { [{ input: [], expected: [] }].forEach(({ input, expected }) => it(`given an empty array, it returns an empty array [${input}]`, () => { - expect(dedupe(input)).toEqual(expected); + expect(dedupe(input)).not.toBe(expected); }) ); // Given an array with no duplicates @@ -34,7 +34,8 @@ describe("dedupe()", () => { ].forEach(({ input, expected }) => it(`should return same input values [${input}] without duplicate`, () => { expect(dedupe(input)).toEqual(expected); - })); + }) + ); // When passed to the dedupe function // Given an array with strings or numbers @@ -45,12 +46,17 @@ describe("dedupe()", () => { { input: [1, 2, 5, 5, "a", 5, 10, 10, "a"], expected: [1, 2, 5, "a", 10] }, { input: ["apple", "banana", "orange", "apple", "banana", 1, 3, 4, 1], - expected: ["apple", "banana", "orange", 1, 3,4], + expected: ["apple", "banana", "orange", 1, 3, 4], }, ].forEach(({ input, expected }) => it(`should return deduplicated array for [${input}]`, () => { expect(dedupe(input)).toEqual(expected); }) ); - + + [{ input: [1, 1, 2], expected: [1, 1, 2] }].forEach((input, expected) => + it("returns a copy not the original array", () => { + expect(dedupe(input)).not.toBe(expected); + }) + ); }); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 29cd6fc6f..34cedad5a 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,13 +1,13 @@ function findMax(elements) { if (!Array.isArray(elements)) return "invalid elements"; - if (elements.length === 0) return Infinity; + if (elements.every((e) => typeof e === "string")) return -Infinity; const number = []; for (let i = 0; i < elements.length; i++) { if (typeof elements[i] === "number" && !Number.isNaN(elements[i])) { number.push(elements[i]); } } - if (number.length === 0) return "invalid elements"; + if (number.length === 0) return -Infinity; let max = number[0]; for (let i = 1; i < number.length; i++) { diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index fbe713014..62617c970 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -17,7 +17,7 @@ const findMax = require("./max.js"); // Then it should return -Infinity // Delete this test.todo and replace it with a test. describe("findMax()", () => { - [{ input: [], expected: Infinity }].forEach(({ input, expected }) => + [{ input: [], expected: -Infinity }].forEach(({ input, expected }) => it(`should return ${expected} for empty [${input}]`, () => { expect(findMax(input)).toEqual(expected); }) @@ -58,10 +58,10 @@ describe("findMax()", () => { // When passed to the max function // Then it should return the largest decimal number - [{ input: [0.5, 0.1, 0.56, 0.8], expected: 0.8 }].forEach( + [{ input: [0.5, 0.1, 0.56, 0.8, "1.2"], expected: 0.8 }].forEach( ({ input, expected }) => it(`should return the largest decimal number from the array`, () => { - expect(findMax(input)).toEqual(expected); + expect(findMax(input)).toBeCloseTo(expected); }) ); @@ -69,7 +69,10 @@ describe("findMax()", () => { // When passed to the max function // Then it should return the max and ignore non-numeric values [ - { input: ["edak", "ofonime", "", "@", -4, 10, 6, 50, -100], expected: 50 }, + { + input: ["edak", "ofonime", "", "@", -4, 10, 6, 50, -100, "300"], + expected: 50, + }, ].forEach(({ input, expected }) => it(`should return max numerical value from the array`, () => { expect(findMax(input)).toEqual(expected); @@ -79,7 +82,7 @@ describe("findMax()", () => { // 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 - [{ input: ["peter", "", "@", "Hi"], expected: "invalid elements" }].forEach( + [{ input: ["peter", "", "@", "Hi"], expected: -Infinity }].forEach( ({ input, expected }) => it(`should return "invalid elements" for non-numeric values`, () => { expect(findMax(input)).toEqual(expected); From 0f5523992bb370e9778107e36dd434c775be686b Mon Sep 17 00:00:00 2001 From: ofonimeedak Date: Sat, 28 Mar 2026 15:03:07 +0000 Subject: [PATCH 3/5] add toStrictEqual --- Sprint-1/implement/dedupe.js | 2 ++ Sprint-1/implement/dedupe.test.js | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index d33bcda89..786c89e75 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -6,6 +6,8 @@ function dedupe(arr) { dedupeArray.push(arr[i]); } } + return dedupeArray; } +console.log(dedupe([1, 1, 2])); module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index b9b221e15..e97e58faa 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -33,7 +33,7 @@ describe("dedupe()", () => { { input: [-1, 7, 1], expected: [-1, 7, 1] }, ].forEach(({ input, expected }) => it(`should return same input values [${input}] without duplicate`, () => { - expect(dedupe(input)).toEqual(expected); + expect(dedupe(input)).toStrictEqual(expected); }) ); @@ -50,13 +50,13 @@ describe("dedupe()", () => { }, ].forEach(({ input, expected }) => it(`should return deduplicated array for [${input}]`, () => { - expect(dedupe(input)).toEqual(expected); + expect(dedupe(input)).toStrictEqual(expected); }) ); - [{ input: [1, 1, 2], expected: [1, 1, 2] }].forEach((input, expected) => + [{ input: [1, 1, 2], expected: [1, 2] }].forEach(({ input, expected }) => it("returns a copy not the original array", () => { - expect(dedupe(input)).not.toBe(expected); + expect(dedupe(input)).toStrictEqual(expected); }) ); }); From 2872c2fc0c27fd40a9cf9c658831f138b2f42052 Mon Sep 17 00:00:00 2001 From: ofonimeedak Date: Sat, 28 Mar 2026 15:03:59 +0000 Subject: [PATCH 4/5] remove dead codes --- Sprint-1/implement/max.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 34cedad5a..30a4e443f 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,6 +1,5 @@ function findMax(elements) { if (!Array.isArray(elements)) return "invalid elements"; - if (elements.every((e) => typeof e === "string")) return -Infinity; const number = []; for (let i = 0; i < elements.length; i++) { if (typeof elements[i] === "number" && !Number.isNaN(elements[i])) { From 1de26d1b623295773db008a9d17679c7812d5700 Mon Sep 17 00:00:00 2001 From: ofonimeedak Date: Sat, 28 Mar 2026 15:04:33 +0000 Subject: [PATCH 5/5] run test for sum --- Sprint-1/implement/sum.js | 13 ++++++------- Sprint-1/implement/sum.test.js | 10 ++++++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 1389977b8..2d2e1050d 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,19 +1,18 @@ function sum(elements) { if (!Array.isArray(elements)) return "invalid elements"; - if(elements.length===0) return 0; + if (elements.length === 0) return 0; const number = []; for (const x of elements) { if (typeof x === "number" && !Number.isNaN(x)) { number.push(x); } } - if(number.length===0) return "invalid elements"; - let sumOfNum=0; - for(let i=0; i { // Then it should ignore the non-numerical values and return the sum of the numerical elements [ - { input: ["evan", 3, "mike", 20, 6, "", "/", , , 20], expected: 49 }, + { + input: ["evan", 3, "mike", 20, 6, "", "/", undefined, null, 20], + expected: 49, + }, ].forEach(({ input, expected }) => it(`should return ${expected} for [${input}]`, () => { expect(sum(input)).toEqual(expected); @@ -67,7 +70,10 @@ describe("sum()", () => { // When passed to the sum function // Then it should return the least surprising value given how it behaves for all other inputs [ - { input: ["evan", "mike", "", "/", , ,], expected: "invalid elements" }, + { + input: ["evan", "mike", "", "/", undefined], + expected: "invalid elements", + }, ].forEach(({ input, expected }) => it(`should return ${expected} for [${input}]`, () => { expect(sum(input)).toEqual(expected);