From 2c983b7a68ad9953ab6e870a7dfb7636e7403108 Mon Sep 17 00:00:00 2001 From: JorvanW Date: Mon, 20 Jul 2026 19:32:43 +0100 Subject: [PATCH 01/11] Updated code for median.js to pass the late expectations --- Sprint-1/fix/median.js | 22 +++++++++++++++---- Sprint-1/fix/median.test.js | 38 ++++++++++++++++----------------- prep/mean.js | 0 prep/mean.test.js | 0 prep/parse-query-string.js | 0 prep/parse-query-string.test.js | 7 ++++++ 6 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 prep/mean.js create mode 100644 prep/mean.test.js create mode 100644 prep/parse-query-string.js create mode 100644 prep/parse-query-string.test.js diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..6b000bd80 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,10 +5,24 @@ // 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) { + list = list.filter(element => typeof element === 'number'); + list.sort((a, b) => a - b); + + if (list.length % 2 === 0){ + const middleIndexR = Math.floor(list.length / 2); + const middleIndexL = middleIndexR - 1 + const evenMedian = (list[middleIndexL] + list[middleIndexR]) / 2; + + return evenMedian + } else { + const middleIndex = Math.floor(list.length / 2); + const median = list[middleIndex]; + return median; + } + } + + module.exports = calculateMedian; diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..b5cda5690 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -27,24 +27,24 @@ describe("calculateMedian", () => { it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) ); - it("doesn't modify the input array [3, 1, 2]", () => { - const list = [3, 1, 2]; - calculateMedian(list); - expect(list).toEqual([3, 1, 2]); - }); +// it("doesn't modify the input array [3, 1, 2]", () => { +// const list = [3, 1, 2]; +// calculateMedian(list); +// expect(list).toEqual([3, 1, 2]); +// }); - [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => - it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) - ); +// [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => +// it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) +// ); - [ - { input: [1, 2, "3", null, undefined, 4], expected: 2 }, - { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 }, - { input: [1, "2", 3, "4", 5], expected: 3 }, - { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 }, - { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 }, - { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 }, - ].forEach(({ input, expected }) => - it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) - ); -}); +// [ +// { input: [1, 2, "3", null, undefined, 4], expected: 2 }, +// { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 }, +// { input: [1, "2", 3, "4", 5], expected: 3 }, +// { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 }, +// { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 }, +// { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 }, +// ].forEach(({ input, expected }) => +// it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) +// ); + }); diff --git a/prep/mean.js b/prep/mean.js new file mode 100644 index 000000000..e69de29bb diff --git a/prep/mean.test.js b/prep/mean.test.js new file mode 100644 index 000000000..e69de29bb diff --git a/prep/parse-query-string.js b/prep/parse-query-string.js new file mode 100644 index 000000000..e69de29bb diff --git a/prep/parse-query-string.test.js b/prep/parse-query-string.test.js new file mode 100644 index 000000000..d4a0f01b9 --- /dev/null +++ b/prep/parse-query-string.test.js @@ -0,0 +1,7 @@ +test("given a query string with no query parameters, returns an empty object", function () { + const input = ""; + const currentOutput = parseQueryString(input); + const targetOutput = {}; + + expect(currentOutput).toEqual(targetOutput); +}); From 6c109e9251cee26c86da5b344a7aa6ec2771f580 Mon Sep 17 00:00:00 2001 From: JorvanW Date: Mon, 20 Jul 2026 19:59:03 +0100 Subject: [PATCH 02/11] Added code to dedupe.js --- Sprint-1/implement/dedupe.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..18b6526b0 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,3 @@ -function dedupe() {} +function dedupe(list) { + return [...new Set(list)]; +} From d57add3962dd24e90393199b101197e4cb9ba53f Mon Sep 17 00:00:00 2001 From: JorvanW Date: Mon, 20 Jul 2026 20:27:43 +0100 Subject: [PATCH 03/11] added code for max.js --- Sprint-1/implement/max.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..e1b256bce 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,8 @@ function findMax(elements) { + elements = elements.filter(element => typeof element === 'number'); + if (elements.length === 0){ + return -Infinity; + } + return Math.max(...elements); } - module.exports = findMax; From e3befbbc298fe45abd00e85daaf92ed20577f06d Mon Sep 17 00:00:00 2001 From: JorvanW Date: Mon, 20 Jul 2026 20:48:37 +0100 Subject: [PATCH 04/11] Added code to sum.js --- Sprint-1/implement/sum.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..ae3530cf5 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,14 @@ function sum(elements) { + elements = elements.filter(element => typeof element === 'number'); + let total = 0; + + for (let element of elements) { + total += element; + // += means to add to the current value and assign as result + // elements are individual items inside a collection (not just string) + } + + return total; } module.exports = sum; From 872ddf0cacc7385d8682ab46cc0fcfe2b375c301 Mon Sep 17 00:00:00 2001 From: JorvanW Date: Tue, 21 Jul 2026 11:01:13 +0100 Subject: [PATCH 05/11] modified code on includes.js to use a for...of loop --- 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..6f2e347ad 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; } @@ -10,4 +9,5 @@ function includes(list, target) { return false; } + module.exports = includes; From d98caf6cb4574caa5753c9e0e9e9eb291b8547a7 Mon Sep 17 00:00:00 2001 From: JorvanW Date: Wed, 22 Jul 2026 11:44:17 +0100 Subject: [PATCH 06/11] Updated the code in median.js --- Sprint-1/fix/median.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 6b000bd80..7a210a3ac 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,6 +6,30 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { + // [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ] + if (typeof list === 'string') { + return null + } + if (typeof list === 'number') { + return null + } + + if (list === null) { + return null + } + if (typeof list === 'undefined') { + return null + } + + if (list === undefined) { + return null + } + if (typeof list === 'object' && !Array.isArray(list)) { + return null + } + if (Array.isArray(list) && list.length === 0){ + return null + } list = list.filter(element => typeof element === 'number'); list.sort((a, b) => a - b); @@ -23,6 +47,6 @@ function calculateMedian(list) { } - +calculateMedian(['apple']) module.exports = calculateMedian; From 3566f9ba6a39aff4bb388b114c3a14e1c56c9cb8 Mon Sep 17 00:00:00 2001 From: JorvanW Date: Wed, 22 Jul 2026 12:40:18 +0100 Subject: [PATCH 07/11] Improved on the code for median.js and added code and tests for dedupe --- Sprint-1/fix/median.js | 3 +++ Sprint-1/fix/median.test.js | 36 +++++++++++++++---------------- Sprint-1/implement/dedupe.js | 2 ++ Sprint-1/implement/dedupe.test.js | 11 +++++++++- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 7a210a3ac..6226fbae0 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -31,6 +31,9 @@ function calculateMedian(list) { return null } list = list.filter(element => typeof element === 'number'); + if (list.length === 0) { + return null + } list.sort((a, b) => a - b); if (list.length % 2 === 0){ diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index b5cda5690..edf57aef9 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -27,24 +27,24 @@ describe("calculateMedian", () => { it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) ); -// it("doesn't modify the input array [3, 1, 2]", () => { -// const list = [3, 1, 2]; -// calculateMedian(list); -// expect(list).toEqual([3, 1, 2]); -// }); + it("doesn't modify the input array [3, 1, 2]", () => { + const list = [3, 1, 2]; + calculateMedian(list); + expect(list).toEqual([3, 1, 2]); + }); -// [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => -// it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) -// ); + [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => + it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) + ); -// [ -// { input: [1, 2, "3", null, undefined, 4], expected: 2 }, -// { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 }, -// { input: [1, "2", 3, "4", 5], expected: 3 }, -// { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 }, -// { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 }, -// { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 }, -// ].forEach(({ input, expected }) => -// it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) -// ); + [ + { input: [1, 2, "3", null, undefined, 4], expected: 2 }, + { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 }, + { input: [1, "2", 3, "4", 5], expected: 3 }, + { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 }, + { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 }, + { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 }, + ].forEach(({ input, expected }) => + it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + ); }); diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 18b6526b0..9853f4766 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,3 +1,5 @@ function dedupe(list) { return [...new Set(list)]; } + +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 d7c8e3d8e..853e7c12d 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,22 @@ E.g. dedupe([1, 2, 1]) returns [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", () => { + 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("given an array with no duplicates, it returns an a copy", () => { + expect(dedupe([1, 2, 3,])).toEqual([1, 2, 3,]); +}); + // Given an array of strings or numbers // When passed to the dedupe function // Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. +test("given an array with duplicate strings and numbers, it returns the original with duplicates removed", () => { + expect(dedupe([1, 'apple', 'horse', 'horse', 2, 2, 3, 'bat'])).toEqual([1, 'apple', 'horse', 2, 3, 'bat']); +}); From 26333593581bb3d02c4620b0f18ed6c701ca54ca Mon Sep 17 00:00:00 2001 From: JorvanW Date: Wed, 22 Jul 2026 13:10:16 +0100 Subject: [PATCH 08/11] added code for max.test.js --- Sprint-1/implement/max.test.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..439e14d14 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,49 @@ 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", () => { + 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, returns the same number", () => { + expect(findMax([4])).toEqual(4); +}); // 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 both positive and negative numbers, returns the largest number", () => { + expect(findMax([10, -10])).toEqual(10); +}); // 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 negative numbers, returns the number closest to zero", () => { + expect(findMax([-3 , -15, -4, -11])).toEqual(-3); +}); + // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("given an array with negative numbers, returns the number closest to zero", () => { + expect(findMax([-3 , -15, -4, -11])).toEqual(-3); +}); // 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, ignore the non numeric values and return the max", () => { + expect(findMax(['car', 'house', 1, 12, 'fish'])).toEqual(12); +}); // 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", () => { + expect(findMax(['car', 'house', 'fish', 'car'])).toEqual(-Infinity); +}); \ No newline at end of file From 11f5bfec40007b2a3e440e8f7dcefc64c5e6de3e Mon Sep 17 00:00:00 2001 From: JorvanW Date: Wed, 22 Jul 2026 13:36:57 +0100 Subject: [PATCH 09/11] added tests for sum.test --- Sprint-1/implement/sum.test.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..e7c00ff6e 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,42 @@ 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", () => { + 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 one number, returns the same number", () => { + expect(sum([7])).toEqual(7); +}); // 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, returns the total sum", () => { + expect(sum([-4, -15, -12])).toEqual(-31); +}); // 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, returns the correct sum", () => { + expect(sum([4.1, 5.4, 7.8])).toEqual(17.3); +}); // 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 with non-number values, ignores the non-number values and returns the correct sum", () => { + expect(sum(['fish', 4, 'dog', 5, ,'pizza', 10])).toEqual(19); +}); // 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 of only non-numeric values, returns 0", () => { + expect(sum(['fish','dog','pizza'])).toEqual(0); +}); From 3773eb140fee33b960cc2a7404258fbc680dcf35 Mon Sep 17 00:00:00 2001 From: JorvanW Date: Wed, 22 Jul 2026 14:31:42 +0100 Subject: [PATCH 10/11] Updated code of sum.js to return null if there are only non-numeric inputs only. --- Sprint-1/implement/sum.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index ae3530cf5..b5d5f44d9 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,8 +1,11 @@ function sum(elements) { elements = elements.filter(element => typeof element === 'number'); - let total = 0; - - for (let element of elements) { + if (elements.length === 0) { + return null;} + + let total = 0; + + for (let element of elements) { total += element; // += means to add to the current value and assign as result // elements are individual items inside a collection (not just string) From 6773b662569105d8c4cc65bb10de66e9a20578dc Mon Sep 17 00:00:00 2001 From: JorvanW Date: Fri, 21 Aug 2026 15:18:04 +0100 Subject: [PATCH 11/11] removed extra files --- prep/mean.js | 0 prep/mean.test.js | 0 prep/parse-query-string.js | 0 prep/parse-query-string.test.js | 7 ------- 4 files changed, 7 deletions(-) delete mode 100644 prep/mean.js delete mode 100644 prep/mean.test.js delete mode 100644 prep/parse-query-string.js delete mode 100644 prep/parse-query-string.test.js diff --git a/prep/mean.js b/prep/mean.js deleted file mode 100644 index e69de29bb..000000000 diff --git a/prep/mean.test.js b/prep/mean.test.js deleted file mode 100644 index e69de29bb..000000000 diff --git a/prep/parse-query-string.js b/prep/parse-query-string.js deleted file mode 100644 index e69de29bb..000000000 diff --git a/prep/parse-query-string.test.js b/prep/parse-query-string.test.js deleted file mode 100644 index d4a0f01b9..000000000 --- a/prep/parse-query-string.test.js +++ /dev/null @@ -1,7 +0,0 @@ -test("given a query string with no query parameters, returns an empty object", function () { - const input = ""; - const currentOutput = parseQueryString(input); - const targetOutput = {}; - - expect(currentOutput).toEqual(targetOutput); -});