From d1e2c7442439f64e8dcddf9aacb1489e6ba229c3 Mon Sep 17 00:00:00 2001 From: Hadi Vahidi Date: Fri, 7 Nov 2025 16:56:23 +0000 Subject: [PATCH 1/8] Sprint 1: Fix median function and complete implement/refactor exercises --- Sprint-1/fix/median.js | 39 +++++++++++++++++++++++++++++++++++--- Sprint-1/package-lock.json | 2 ++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..6b4ff09b9 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,42 @@ // 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; + } + + for (let i = 0; i < numbers.length; i++) { + for (let j = i + 1; j < numbers.length; j++) { + if (numbers[i] > numbers[j]) { + const temp = numbers[i]; + numbers[i] = numbers[j]; + numbers[j] = temp; + } + } + } + + const middleIndex = Math.floor(numbers.length / 2); + + + if (numbers.length % 2 === 0) { + + return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; + } else { + + return numbers[middleIndex]; + } } + module.exports = calculateMedian; diff --git a/Sprint-1/package-lock.json b/Sprint-1/package-lock.json index 83e427d0b..b52480af5 100644 --- a/Sprint-1/package-lock.json +++ b/Sprint-1/package-lock.json @@ -56,6 +56,7 @@ "integrity": "sha512-Oixnb+DzmRT30qu9d3tJSQkxuygWm32DFykT4bRoORPa9hZ/L4KhVB/XiRm6KG+roIEM7DBQlmg27kw2HZkdZg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.25.7", @@ -1368,6 +1369,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001663", "electron-to-chromium": "^1.5.28", From 9651da664f2e8f6fbc368b85ac474dba8ea9d40c Mon Sep 17 00:00:00 2001 From: Hadi Vahidi Date: Wed, 12 Nov 2025 21:01:05 +0000 Subject: [PATCH 2/8] Implement findMax function to return the largest number in an array --- Sprint-1/implement/max.js | 17 +++++++++++++++++ Sprint-1/package-lock.json | 2 -- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..15adb4c1b 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,21 @@ function findMax(elements) { + if (elements.length === 0) { + return -Infinity; + } + + let max = -Infinity; + + for (let i = 0; i < elements.length; i++) { + let current = elements[i]; + + if (typeof current === "number") { + if (current > max) { + max = current; + } + } + } + + return max; } module.exports = findMax; diff --git a/Sprint-1/package-lock.json b/Sprint-1/package-lock.json index b52480af5..83e427d0b 100644 --- a/Sprint-1/package-lock.json +++ b/Sprint-1/package-lock.json @@ -56,7 +56,6 @@ "integrity": "sha512-Oixnb+DzmRT30qu9d3tJSQkxuygWm32DFykT4bRoORPa9hZ/L4KhVB/XiRm6KG+roIEM7DBQlmg27kw2HZkdZg==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.25.7", @@ -1369,7 +1368,6 @@ } ], "license": "MIT", - "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001663", "electron-to-chromium": "^1.5.28", From 22655f0a2329f0645a1a553a6439361017eadcd5 Mon Sep 17 00:00:00 2001 From: Hadi Vahidi Date: Wed, 12 Nov 2025 21:04:55 +0000 Subject: [PATCH 3/8] Implement sum function to add numbers in an array --- Sprint-1/implement/sum.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..06164b255 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,19 @@ function sum(elements) { + // This variable will store the total sum + let total = 0; + + // Go through each element in the array + for (let i = 0; i < elements.length; i++) { + let current = elements[i]; // get the current element + + // Only add it if it's a number + if (typeof current === "number") { + total = total + current; // add to total + } + } + + // Return the sum of all numbers + return total; } module.exports = sum; From 9f0d9c4ddece438bf98c5bf05f3ce4d6f7fa709d Mon Sep 17 00:00:00 2001 From: Hadi Vahidi Date: Wed, 12 Nov 2025 21:40:35 +0000 Subject: [PATCH 4/8] Refactor includes function to use for...of loop --- Sprint-1/refactor/includes.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..3001fa212 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +1,12 @@ // 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; + return true; } } - return false; + return false; } module.exports = includes; From f6f580c20b16d7fed1bd8fefeeaf6e7e4a5d807c Mon Sep 17 00:00:00 2001 From: Hadi Vahidi Date: Sat, 15 Nov 2025 11:28:09 +0000 Subject: [PATCH 5/8] wrote the test for sum.js and updated the sum function to check if the input is not NaN --- Sprint-1/implement/sum.js | 2 +- Sprint-1/implement/sum.test.js | 42 +++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 06164b255..66f0a3dda 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -7,7 +7,7 @@ function sum(elements) { let current = elements[i]; // get the current element // Only add it if it's a number - if (typeof current === "number") { + if (typeof current === "number" && !Number.isNaN(current)) { total = total + current; // add to total } } diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..ecfc8e28d 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,64 @@ 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 currentOutput = sum([]); + const targetPitPut= 0; + + expect(currentOutput).toEqual(targetPitPut); +}) + // Given an array with just one number // When passed to the sum function // Then it should return that number +test("given an array whit only 1 number, returns same input",()=>{ + const currentOutput = sum([4]); + const targetPitPut= 4; + + expect(currentOutput).toEqual(targetPitPut); +}) + // 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 whit a negative number, returns correct total (subtract the negative number)",()=>{ + const currentOutput = sum([1,2,3,4,-5,]); + const targetPitPut= 5; + + expect(currentOutput).toEqual(targetPitPut); +}) + // 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 whit a decimal/float number, returns correct total (decimal/float number)",()=>{ + const currentOutput = sum([1,2,3,4,3.5,]); + const targetPitPut= 13.5; + + expect(currentOutput).toEqual(targetPitPut); +}) + + // 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 whit a non-number values, returns correct total (ignore the NaN and sum the others)",()=>{ + const currentOutput = sum([1,2,3,4,"hi",5]); + const targetPitPut= 15; + + expect(currentOutput).toEqual(targetPitPut); +}) // 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, returns the least surprising value (0)", () => { + const currentOutput = sum(["hello", null, undefined, {}, [], true, NaN]); + const targetOutput = 0; + + expect(currentOutput).toEqual(targetOutput); +}); From 8a4b703d3bde2f45faf7602971522ead104bb038 Mon Sep 17 00:00:00 2001 From: Hadi Vahidi Date: Sat, 15 Nov 2025 11:51:07 +0000 Subject: [PATCH 6/8] added the test for the max function --- Sprint-1/implement/max.test.js | 49 +++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..9ed7f3365 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,75 @@ 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", () => { + const currentOutput = findMax([]); + const targetOutput = -Infinity; + + expect(currentOutput).toEqual(targetOutput); + }); // 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", () => { + const currentOutput = findMax([42]); + const targetOutput = 42; + + expect(currentOutput).toEqual(targetOutput); +}); + + // 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 an array with positive and negative numbers, returns the largest", () => { + const currentOutput = findMax([-25, 5, 20, -3, 15]); + const targetOutput = 20; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("given an array of only negative numbers, returns the closest to zero", () => { + const currentOutput = findMax([-50, -3, -20, -10]); + const targetOutput = -3; + + expect(currentOutput).toEqual(targetOutput); +}); + // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number + test("given an array of decimal numbers, returns the largest decimal", () => { + const currentOutput = findMax([1.1, 3.5, 2.9, 3.4]); + const targetOutput = 3.5; + + expect(currentOutput).toEqual(targetOutput); +}); + // 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("given an array with non-number values, returns correct max (ignoring non-numbers)", () => { + const currentOutput = findMax([10, "hi", 50, true, 3]); + const targetOutput = 50; + + expect(currentOutput).toEqual(targetOutput); +}); + // 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 an array with only non-number values, returns -Infinity", () => { + const currentOutput = findMax(["a", null, undefined, {}, [], true]); + const targetOutput = -Infinity; + + expect(currentOutput).toEqual(targetOutput); +}); From 5c3214a5a5429aba110f7eddd8a07d36c9799e72 Mon Sep 17 00:00:00 2001 From: Hadi Vahidi Date: Sat, 15 Nov 2025 12:08:47 +0000 Subject: [PATCH 7/8] implement the dedupe function and tests --- Sprint-1/implement/dedupe.js | 16 +++++++++++++++- Sprint-1/implement/dedupe.test.js | 22 ++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..a0055e476 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,15 @@ -function dedupe() {} +function dedupe(elements) { + const unique = []; + + for (let i = 0; i < elements.length; i++) { + const current = elements[i]; + + if (!unique.includes(current)) { + unique.push(current); + } + } + + return unique; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..c591fca74 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,30 @@ 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 currentOutput = dedupe([]); + const targetOutput = []; + + expect(currentOutput).toEqual(targetOutput); +}); // 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 an the same array", () => { + const currentOutput = dedupe([1,4,6,"d","a","x","e",0,7,8]); + const targetOutput = [1,4,6,"d","a","x","e",0,7,8]; + + expect(currentOutput).toEqual(targetOutput); +}); // 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 or numbers, it returns an the array whit no duplicated elements", () => { + const currentOutput = dedupe(['a','a','a','b','b','c']); + const targetOutput = ['a','b','c']; + + expect(currentOutput).toEqual(targetOutput); +}); \ No newline at end of file From 8b522deba044ee05498b19244d0b5849e7b17a0b Mon Sep 17 00:00:00 2001 From: Hadi Vahidi Date: Sat, 13 Dec 2025 14:38:46 +0000 Subject: [PATCH 8/8] updated the code base on the general feedbacks --- Sprint-1/implement/dedupe.test.js | 13 +++++++++++-- Sprint-1/implement/max.js | 3 ++- Sprint-1/implement/sum.js | 3 ++- Sprint-1/implement/sum.test.js | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index c591fca74..2effce8a2 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -28,11 +28,20 @@ test("given an empty array, it returns an empty array", () => { // Then it should return a copy of the original array test("Given an array with no duplicates, it returns an the same array", () => { - const currentOutput = dedupe([1,4,6,"d","a","x","e",0,7,8]); + const input = [1,4,6,"d","a","x","e",0,7,8]; + const currentOutput = dedupe(input); const targetOutput = [1,4,6,"d","a","x","e",0,7,8]; expect(currentOutput).toEqual(targetOutput); }); + +test("The copy must not be the very same array in memory", () => { + const input = [3, 1, 2]; + const currentOutput = dedupe(input); + + expect(currentOutput).toEqual([3, 1, 2]); + expect(currentOutput).not.toBe(input); // make sure the function returns a copy +}); // Given an array with strings or numbers // When passed to the dedupe function // Then it should remove the duplicate values, preserving the first occurrence of each element @@ -42,4 +51,4 @@ test("Given an array with strings or numbers, it returns an the array whit no du const targetOutput = ['a','b','c']; expect(currentOutput).toEqual(targetOutput); -}); \ No newline at end of file +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 15adb4c1b..18c819cd2 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -8,7 +8,8 @@ function findMax(elements) { for (let i = 0; i < elements.length; i++) { let current = elements[i]; - if (typeof current === "number") { + // using Number.isFinite keeps NaN or Infinity out of the comparison + if (typeof current === "number" && Number.isFinite(current)) { if (current > max) { max = current; } diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 66f0a3dda..317b386f9 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -7,7 +7,8 @@ function sum(elements) { let current = elements[i]; // get the current element // Only add it if it's a number - if (typeof current === "number" && !Number.isNaN(current)) { + // Number.isFinite skips NaN and Infinity, so only real numbers add to total + if (typeof current === "number" && Number.isFinite(current)) { total = total + current; // add to total } } diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index ecfc8e28d..f6ae98d1f 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -51,7 +51,7 @@ test("given an array whit a decimal/float number, returns correct total (decima const currentOutput = sum([1,2,3,4,3.5,]); const targetPitPut= 13.5; - expect(currentOutput).toEqual(targetPitPut); + expect(currentOutput).toBeCloseTo(targetPitPut); // allow tiny floating point differences })