From d5f3e06218310d710db44f6d81c55b2dce714715 Mon Sep 17 00:00:00 2001 From: Fida H Ali Zada Date: Sat, 7 Mar 2026 19:48:27 +0000 Subject: [PATCH 01/12] Implemented calculateMedian function --- Sprint-1/fix/median.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..ccbf9717a 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; + + // first of all, checking if 'list' is an array + if (Array.isArray(list)) { + + const filteredArr = list.filter(x => typeof x === 'number'); + + if (filteredArr.length < 2) { + return null; + } + + // using 'spread operator' copies the array without mutating it + const sortedList = [...filteredArr].sort((a, b) => a - b); + + const middleIndex = Math.floor(sortedList.length / 2); + + if (sortedList.length % 2 === 0) { + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } else { + return sortedList[middleIndex] + } + } + return null; } module.exports = calculateMedian; From 868c5b9ab67ddbbd733b26ab3727d0b7ce01529a Mon Sep 17 00:00:00 2001 From: Fida H Ali Zada Date: Sat, 7 Mar 2026 20:35:37 +0000 Subject: [PATCH 02/12] fixed findMax function --- Sprint-1/implement/max.js | 8 ++++++++ Sprint-1/implement/max.test.js | 32 +++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..7a5c0054e 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,12 @@ function findMax(elements) { + const filteredArr = elements.filter(x => typeof x === 'number'); + + if (filteredArr.length == 0) { + return -Infinity; + } + + // using 'spread sytax' + return Math.max(...filteredArr); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..7c2f86699 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,58 @@ 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("returns -Infinity for an empty array", () => { + expect(findMax([])).toEqual(-Infinity); +}) // 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, should return that number", () => { + expect(findMax([8])).toEqual(8); + expect(findMax([-5])).toEqual(-5); + expect(findMax([0])).toEqual(0); +}) + // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +test("return the largest number overall", () => { + expect(findMax([-8, -4, 0, 4, 8])).toEqual(8); + expect(findMax([-3, -2, -1, 4, 2, 3])).toEqual(4); +}) + // 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 with only negative numbers, should return closest to 0", () => { + expect(findMax([-2, -4, -1, -3, -100])).toEqual(-1); +}) + // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("an array with decimal numbers, should return the largest decimal number", () => { + expect(findMax([0.1, 0.2, 0.9, 0.8, 0.3, 0.7, 0.4, 0.6])).toEqual(0.9); +}) + // 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("ignore the non-numeric values", () => { + expect(findMax(["Blue", 3, "White", "Orange", "Pink"])).toEqual(3); +}) + // 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("an array with only non-number values", () => { + expect(findMax(["Blue", "White", "Orange", "Pink"])).toEqual(-Infinity); +}) From 08758cb1b549a5dc5175c9a0b4c9033030ecdd86 Mon Sep 17 00:00:00 2001 From: Fida H Ali Zada Date: Sat, 7 Mar 2026 20:37:11 +0000 Subject: [PATCH 03/12] fixed sum function and wrote tests --- Sprint-1/implement/sum.js | 5 +++++ Sprint-1/implement/sum.test.js | 27 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..51a3a0a9e 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,9 @@ function sum(elements) { + + // const filteredArr = elements.filter(x => typeof x === 'number'); + // return filteredArr.reduce((a, b) => a + b, 0); + + return elements.filter(x => typeof x === 'number').reduce((a, b) => a + b, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..a6993d202 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,49 @@ 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("return 0 for an empty array", () => { + expect(sum([])).toEqual(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, return that number", () => { + expect(sum([3])).toEqual(3); + expect(sum([-3])).toEqual(-3); + expect(sum([0])).toEqual(0); +}) + // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("return the correct total when passed negative numbers", () => { + expect(sum([-3, -6, -1])).toEqual(-10); +}) + // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("decimal number arrays", () => { + expect(sum([0.2, 0.4, 0.2])).toEqual(0.8); +}) // 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("an array with non-numbers", () => { + expect(sum([2, "Blue", 3, "Black", "Green"])).toEqual(5); +}) + // 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("an array with non-number values only", () => { + expect(sum([true, false, "Black"])).toEqual(0); + expect(sum([undefined, null, "Black"])).toEqual(0); +}) From 0c5f55ec6cced9cdd7c848b795280b022840e1a3 Mon Sep 17 00:00:00 2001 From: Fida H Ali Zada Date: Sat, 7 Mar 2026 20:53:34 +0000 Subject: [PATCH 04/12] fixed dedupe function and wrote tests --- Sprint-1/implement/dedupe.js | 14 +++++++++++++- Sprint-1/implement/dedupe.test.js | 13 ++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..f47d5ce9c 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,13 @@ -function dedupe() {} +function dedupe(array) { + let arr = array.filter((item, index) => { + + // indexOf() returns the first index where that value appears in the array. + if (array.indexOf(item) === index) { + return item; + } + }) + + return arr; +} + +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..c05c8721b 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,23 @@ 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("return an empty array for 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("if no duplicates, return the copy of the original array", () => { + expect(dedupe([1, 3, 5, 7])).toEqual([1, 3, 5, 7]); +}) + // 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("an array with strings strings or numbers", () => { + expect(dedupe([2, 2, 3, 3, 3, "Black", "Black", "Black", "Green"])).toEqual([2, 3, "Black", "Green"]); +}) From f978ff57cf6bc373f48d07ce56f71c8b48226b32 Mon Sep 17 00:00:00 2001 From: Fida H Ali Zada Date: Sat, 7 Mar 2026 20:58:41 +0000 Subject: [PATCH 05/12] 'includes' function refactured --- Sprint-1/refactor/includes.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..2e1678917 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,8 @@ // 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 182d369da761bb82d8de626b70ca213cbf10596b Mon Sep 17 00:00:00 2001 From: Fida H Ali Zada Date: Sat, 7 Mar 2026 21:13:22 +0000 Subject: [PATCH 06/12] stretch exercise done --- Sprint-1/stretch/aoc-2018-day1/solution.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..feb9593b5 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,3 @@ +function sum(list) { + return list.reduce((a, b) => a + b, 0); +} \ No newline at end of file From b612d99c04eaef50f07b770848ae5bf9823016f7 Mon Sep 17 00:00:00 2001 From: Fida Ali Zada Date: Tue, 24 Mar 2026 10:55:52 +0000 Subject: [PATCH 07/12] fixed median function issues --- Sprint-1/fix/median.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index ccbf9717a..0bd092a95 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -7,27 +7,26 @@ function calculateMedian(list) { - // first of all, checking if 'list' is an array - if (Array.isArray(list)) { + // first of all, checking if 'list' isn't an array + if (!Array.isArray(list)) return null; - const filteredArr = list.filter(x => typeof x === 'number'); + const numbers = list.filter(Number.isFinite); - if (filteredArr.length < 2) { - return null; - } + if (numbers.length === 0) return null; + if (numbers.length === 1) return numbers[0]; + // using 'spread operator' copies the array without mutating it - const sortedList = [...filteredArr].sort((a, b) => a - b); + numbers.sort((a, b) => a - b); - const middleIndex = Math.floor(sortedList.length / 2); + const middleIndex = Math.floor(numbers.length / 2); - if (sortedList.length % 2 === 0) { - return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + if (numbers.length % 2 === 0) { + return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; } else { - return sortedList[middleIndex] + return numbers[middleIndex] } - } - return null; + } module.exports = calculateMedian; From 7c10e7699337a27b6953d8158f7fea8095be2cd5 Mon Sep 17 00:00:00 2001 From: Fida Ali Zada Date: Tue, 24 Mar 2026 11:34:43 +0000 Subject: [PATCH 08/12] changed the implementation --- Sprint-1/implement/dedupe.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index f47d5ce9c..b6db3188a 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,13 +1,15 @@ function dedupe(array) { - let arr = array.filter((item, index) => { + // let arr = array.filter((item, index) => { - // indexOf() returns the first index where that value appears in the array. - if (array.indexOf(item) === index) { - return item; - } - }) + // // indexOf() returns the first index where that value appears in the array. + // if (array.indexOf(item) === index) { + // return item; + // } + // }) - return arr; + // return arr; + + return [...new Set(array)]; } module.exports = dedupe; \ No newline at end of file From dacc81f63fe5202214c3cdaad118129d7a7b4e6a Mon Sep 17 00:00:00 2001 From: Fida Ali Zada Date: Tue, 24 Mar 2026 11:35:23 +0000 Subject: [PATCH 09/12] changed the implementation in max and max.test --- Sprint-1/implement/max.js | 7 +++---- Sprint-1/implement/max.test.js | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 7a5c0054e..adb83b4db 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,12 +1,11 @@ function findMax(elements) { - const filteredArr = elements.filter(x => typeof x === 'number'); + const numbers = elements.filter(Number.isFinite); - if (filteredArr.length == 0) { + if (numbers.length == 0) { return -Infinity; } - // using 'spread sytax' - return Math.max(...filteredArr); + return Math.max(...numbers); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 7c2f86699..097d79edf 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -61,7 +61,7 @@ test("an array with decimal numbers, should return the largest decimal number", // Then it should return the max and ignore non-numeric values test("ignore the non-numeric values", () => { - expect(findMax(["Blue", 3, "White", "Orange", "Pink"])).toEqual(3); + expect(findMax(["Blue", 3, "400", "2", "White", "Orange", "Pink"])).toEqual(3); }) // Given an array with only non-number values From b62ff40248b40fc0650e5a39e50d90f14d167c12 Mon Sep 17 00:00:00 2001 From: Fida Ali Zada Date: Tue, 24 Mar 2026 11:35:57 +0000 Subject: [PATCH 10/12] changed the implementation in the sum.js and test method in sum.test.js --- Sprint-1/implement/sum.js | 2 +- Sprint-1/implement/sum.test.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 51a3a0a9e..fafb010c1 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -3,7 +3,7 @@ function sum(elements) { // const filteredArr = elements.filter(x => typeof x === 'number'); // return filteredArr.reduce((a, b) => a + b, 0); - return elements.filter(x => typeof x === 'number').reduce((a, b) => a + b, 0); + return elements.filter(Number.isFinite).reduce((a, b) => a + b, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a6993d202..141cce0d6 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -40,7 +40,8 @@ test("return the correct total when passed negative numbers", () => { // When passed to the sum function // Then it should return the correct total sum test("decimal number arrays", () => { - expect(sum([0.2, 0.4, 0.2])).toEqual(0.8); + expect(sum([0.2, 0.4, 0.2])).toBeCloseTo(0.8); + expect(sum([1.2, 0.6, 0.005])).toBeCloseTo(1.805); }) // Given an array containing non-number values From f51817c013dd33eb860950ab6e44e51ab4607c33 Mon Sep 17 00:00:00 2001 From: Fida Ali Zada Date: Tue, 24 Mar 2026 11:44:51 +0000 Subject: [PATCH 11/12] Nodejs File System (fs) issue solved --- Sprint-1/stretch/aoc-2018-day1/solution.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index feb9593b5..01a625aea 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -1,3 +1,12 @@ -function sum(list) { - return list.reduce((a, b) => a + b, 0); -} \ No newline at end of file +const fs = require('fs'); + +const data = fs.readFileSync('input.txt', 'utf-8'); + +// split by spaces or new lines +// filter out empty values +// convert strings into numbers +const numbers = data.split(/\s+/).filter(Boolean).map(Number); + +const sum = numbers.reduce((a, b) => a + b, 0); + +console.log(sum); // 529 \ No newline at end of file From e9ae929dad2ada8d517f16530393ae3b9adcbfbe Mon Sep 17 00:00:00 2001 From: Fida Ali Zada Date: Sun, 29 Mar 2026 13:27:02 +0100 Subject: [PATCH 12/12] dedupe test --- Sprint-1/implement/dedupe.test.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index c05c8721b..e014510d9 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -26,7 +26,11 @@ test("return an empty array for an empty array", () => { // Then it should return a copy of the original array test("if no duplicates, return the copy of the original array", () => { - expect(dedupe([1, 3, 5, 7])).toEqual([1, 3, 5, 7]); + const input = [1, 3, 5, 7]; + const result = dedupe(input); + + expect(result).toEqual([1, 3, 5, 7]); + expect(result).not.toBe([1, 3, 5, 7]); }) // Given an array with strings or numbers