From 40539fd9e202360e7b8864de7e7fc4e43aaebab8 Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Wed, 22 Oct 2025 01:04:59 +0100 Subject: [PATCH 1/7] finished median.js exercise --- Sprint-1/fix/median.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..fd6cdfc35 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,10 +5,36 @@ // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) // 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; +function calculateMedian(list) { + let newList = 0; + + // validate input + if (list === null || !Array.isArray(list) || !list[0]) + return null; + + // filter out non-numeric values inside the list + list = list.filter(item => Number.isInteger(item)); + + // to avoid dealing with empty arrays + if (list.length === 0) + return null; + + // sort the list + list.sort((a, b) => a - b); + + if ( list.length % 2 == 0 ) { // even + // return the sum of the two middle numbers divided by 2 + const middleIndex = list.length / 2; + newList = list[middleIndex - 1] + list[middleIndex]; + newList = newList / 2; + return newList; + } // else will be odd + + // just return the middle number + newList = list[Math.floor(list.length / 2)]; + + return newList; } +console.log(calculateMedian(["apple", null, undefined])); module.exports = calculateMedian; From d2e4bd25ac2766c692b123a87a61672ea1adef02 Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Wed, 22 Oct 2025 01:34:31 +0100 Subject: [PATCH 2/7] created the function dedupe & sat test cases --- Sprint-1/implement/dedupe.js | 8 +++++++- Sprint-1/implement/dedupe.test.js | 20 ++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..8e2dd1803 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,7 @@ -function dedupe() {} +function dedupe(arr) { + const newArr = new Set(arr); // Using `new Set()` to store only unique values in newArr + return Array.from(newArr); // return athe new array using `Array.from()` +} + +console.log(dedupe(['a','V','a','b','b','c'])); // ['a','b','c'] +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..5a8f1ef7a 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,28 @@ 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.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.todo("given an empty with no duplicates, it returns a copy of the original array"); +test("given an array with no duplicates, it returns a copy of the original array", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + expect(dedupe(['a', 'b', 'c'])).toEqual(['a', 'b', 'c']); + expect(dedupe(['H', 'E', 'L', 'O', '2', '0'])).toEqual(['H', 'E', 'L', 'O', '2', '0']); + expect(dedupe([5])).toEqual([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 +//test.todo("given an array with strings or numbers, it should remove duplicate values, preserving the first occurence of each element"); +test("given an array with strings or numbers, it should remove duplicate values, preserving the first occurence of each element", () => { + expect(dedupe(['a', 'V', 'a', 'b', 'b', 'c'])).toEqual(['a', 'V', 'b', 'c']); + expect(dedupe(['x', 'y', 'x', 'z', 'y', 'x'])).toEqual(['x', 'y', 'z']); + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); From 3b9b03f45df9dfa37b1a6751e916c2e8ffb55274 Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Wed, 22 Oct 2025 22:02:17 +0100 Subject: [PATCH 3/7] finished findMax() and its case tests --- Sprint-1/implement/dedupe.js | 4 ++-- Sprint-1/implement/max.js | 9 ++++++++ Sprint-1/implement/max.test.js | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 8e2dd1803..0d0c249c1 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,6 +1,6 @@ function dedupe(arr) { - const newArr = new Set(arr); // Using `new Set()` to store only unique values in newArr - return Array.from(newArr); // return athe new array using `Array.from()` + arr = new Set(arr); // Using `new Set()` to store only unique values in the same variable + return Array.from(arr); // return the new array using `Array.from()` } console.log(dedupe(['a','V','a','b','b','c'])); // ['a','b','c'] diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..a1e861a68 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,13 @@ function findMax(elements) { + let max = -Infinity; // smallest possible value in JS + for (const element of elements) { + // Check if the element is a finite number and greater than current max + if (Number.isFinite(element) && element > max) { + max = element; // will always be the greatest so far + } + } + return max; } +//console.log(findMax([1.5, 2.3, 0.7, 3.9, 2.8])); // should return 3.9 module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..53cdf98c4 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -17,27 +17,65 @@ const findMax = require("./max.js"); // 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", () => { + expect(findMax([])).toBe(-Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test.todo("given an array with one number, returns that number"); +test("given an array with one number, returns that number", () => { + expect(findMax([7842])).toBe(7842); + expect(findMax([337])).toBe(337); + expect(findMax([1])).toBe(1); +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +test.todo("given an array with both positive and negative numbers, returns the largest number overall"); +test("given an array with both positive and negative numbers, returns the largest number overall", () => { + expect(findMax([-10, 0, 5, -3, 8, 2])).toBe(8); + expect(findMax([-20, -5, -1, -15])).toBe(-1); + expect(findMax([3, 7, 2, 9, 4])).toBe(9); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test.todo("given an array with just negative numbers, returns the closest one to zero"); +test("given an array with just negative numbers, returns the closest one to zero", () => { + expect(findMax([-8, -3, -15, -1, -7])).toBe(-1); + expect(findMax([-50, -20, -30, -10])).toBe(-10); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test.todo("given an array with decimal numbers, returns the largest decimal number"); +test("given an array with decimal numbers, returns the largest decimal number", () => { + expect(findMax([1.5, 2.3, 0.7, 3.9, 2.8])).toBe(3.9); + expect(findMax([-1.2, -0.5, -3.4, -0.1])).toBe(-0.1); + expect(findMax([0.1, 0.01, 0.001, 0.0001])).toBe(0.1); +}); // 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.todo("given an array with non-number values, returns the max and ignores non-numeric values"); +test("given an array with non-number values, returns the max and ignores non-numeric values", () => { + expect(findMax([10, 'hello', 25, null, 15])).toBe(25); + expect(findMax(['world', -5, -10, undefined, -2])).toBe(-2); + expect(findMax([3.5, 'test', 4.2, {}, 2.8])).toBe(4.2); +}); // 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.todo("given an array with only non-number values, returns -Infinity"); +test("given an array with only non-number values, returns -Infinity", () => { + expect(findMax(['a', 'b', 'c'])).toBe(-Infinity); + expect(findMax([null, undefined, {}])).toBe(-Infinity); + expect(findMax([true, false, []])).toBe(-Infinity); +}); \ No newline at end of file From b7e8adf8e6b219b78bd1b87749ca7cf6a037959f Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Wed, 22 Oct 2025 22:36:28 +0100 Subject: [PATCH 4/7] Implemented sum() and set the test cases --- Sprint-1/implement/sum.js | 10 ++++++++++ Sprint-1/implement/sum.test.js | 36 ++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..d8e813311 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,14 @@ function sum(elements) { + let sum = 0; + + for ( const element of elements) { + if (Number.isFinite(element)) { + sum += element; + } + } + + return sum; } +console.log(sum([0.5, 2.5, 3.5])); module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..6a31c2646 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,56 @@ 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.todo("given an empty array, returns 0"); +test( "given an empty array, returns 0", () => { + expect(sum([])).toEqual(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +test.todo("Given an array with just one number, return that number"); +test( "Given an array with just one number, return that number", () => { + expect(sum([4] )).toEqual(4 ); + expect(sum([99])).toEqual(99); + expect(sum([81])).toEqual(81); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test.todo("Given an array containing negative numbers, return the correct total sum"); +test( "Given an array containing negative numbers, return the correct total sum", () => { + expect(sum([-81, -4, 44, -12])).toEqual(-53); + expect(sum([-10, 20, -30, 40])).toEqual(20); + expect(sum([-5, -15, -10])).toEqual(-30); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test.todo("Given an array with decimal/float numbers, return the correct total sum"); +test( "Given an array with decimal/float numbers, return the correct total sum", () => { + expect(sum([0.5, 2.5, 3.5])).toEqual(6.5); + expect(sum([1.1, 2.2, 3.3])).toEqual(6.6); + expect(sum([10.5, 20.25, 30.75])).toEqual(61.5); +}); // 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.todo("Given an array containing non-number values, ignore them and return the sum of the numerical elements"); +test( "Given an array containing non-number values, ignore them and return the sum of the numerical elements", () => { + expect(sum(['hey', 10, 'hi', 60, 10])).toEqual(80); + expect(sum([null, 5, undefined, 15, 'test'])).toEqual(20); + expect(sum([true, 20, false, 30, 'hello'])).toEqual(50); +}); // 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.todo("Given an array with only non-number values, returns 0"); +test( "Given an array with only non-number values, returns 0", () => { + expect(sum(['a', 'b', 'c'])).toEqual(0); + expect(sum([null, undefined, 'test'])).toEqual(0); + expect(sum([true, false, 'hello'])).toEqual(0); +}); From 98abdd3f320c4d3bc29b10bebdcc0f9af9d855f0 Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Thu, 23 Oct 2025 10:50:34 +0100 Subject: [PATCH 5/7] refactored includes() in includes.js --- Sprint-1/refactor/includes.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..e8ad5f119 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +1,9 @@ // 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; - } - } - return false; +const includes = (list, target) =>{ + for (item of list) + if ( item == target ) return true; + return false; } module.exports = includes; From f2036d32a420bea434788c7ccd41a5a540077b14 Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Tue, 4 Nov 2025 10:32:23 +0000 Subject: [PATCH 6/7] declared var inside for..of loop --- Sprint-1/refactor/includes.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index e8ad5f119..56de43806 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,9 +1,11 @@ // Refactor the implementation of includes to use a for...of loop +// "use strict"; const includes = (list, target) =>{ - for (item of list) + for (const item of list) if ( item == target ) return true; return false; } +console.log(includes([1,2,3,4,5], 3)); module.exports = includes; From 63bcec90f6cbd7fcfe5e279d88ab826cf1b8543d Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Tue, 4 Nov 2025 10:54:28 +0000 Subject: [PATCH 7/7] used isFinite() instead of isInteger() to include floating-point numbers --- Sprint-1/fix/median.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index fd6cdfc35..bea736111 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -13,7 +13,7 @@ function calculateMedian(list) { return null; // filter out non-numeric values inside the list - list = list.filter(item => Number.isInteger(item)); + list = list.filter(item => Number.isFinite(item)); // to avoid dealing with empty arrays if (list.length === 0)