From 8c332a3f6f802da37e647df108b0078bf11df79c Mon Sep 17 00:00:00 2001 From: Alex Jamshidi Date: Tue, 12 May 2026 16:25:51 +0100 Subject: [PATCH 1/7] added solution to median.js --- Sprint-1/fix/median.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..98c165cce 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,13 @@ // 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;} + let numberArray = list.filter(n => Number.isFinite(n)) + if (numberArray.length == 0) {return null}; + numberArray.sort((a, b) => a - b) + + if (numberArray.length % 2 == 1) {return numberArray[((numberArray.length - 1) / 2)]} + else {return (numberArray[((numberArray.length / 2))] + numberArray[((numberArray.length / 2) - 1)]) / 2} } module.exports = calculateMedian; From 5ae1e952db66f35af91e0bdb313d8a9def4dc96b Mon Sep 17 00:00:00 2001 From: Alex Jamshidi Date: Tue, 12 May 2026 22:43:19 +0100 Subject: [PATCH 2/7] added tests for implement dedupe --- Sprint-1/implement/dedupe.js | 6 +++++- Sprint-1/implement/dedupe.test.js | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..bf8a5866c 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(array) { + return [1, 2, 3] +} + +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..acc42cc42 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,39 @@ 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"); +describe("dedupe", () => { + [ + { input: [], expected: [] }, + ].forEach(({ input, expected }) => + it(`returns empty array for [${input}]`, () => 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 + [ + { input: [1, 2, 3], expected: [1, 2, 3] }, + { input: [3, 2, 1, 4, 5], expected: [3, 2, 1, 4, 5] }, + { input: ["hello", "a", "b", "c"], expected: ["hello", "a", "b", "c"] }, + { input: [1, 2, "hello", 4, "a", 6], expected: [1, 2, "hello", 4, "a", 6] }, + ].forEach(({ input, expected }) => + it(`returns the median for [${input}]`, () => expect(dedupe(input)).toEqual(expected)) + ); + // 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. + + [ + { input: [1, 1, 2, 2, 3, 3,], expected: [1, 2, 3] }, + { input: ["a", "a", "b", "b", "c", "c"], expected: ["a", "b", "c"] }, + { input: ["qwe", "qwe", 3, 3, "hello", "hello" , "hello"], expected: ["qwe", 3, "hello"] }, + { input: ["a", "a", "a", "a"], expected: ["a"] }, + { input: [1, 2, 2, 2, 2, 2], expected: [1, 2] }, + ].forEach(({ input, expected }) => + it(`returns the median for [${input}]`, () => expect(dedupe(input)).toEqual(expected)) + ); +}); \ No newline at end of file From 81f05967ea0e838294e32169af57f1946117ae0b Mon Sep 17 00:00:00 2001 From: Alex Jamshidi Date: Tue, 12 May 2026 22:51:00 +0100 Subject: [PATCH 3/7] added solution to implement dedupe --- Sprint-1/implement/dedupe.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index bf8a5866c..b4b3d6142 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,5 +1,11 @@ function dedupe(array) { - return [1, 2, 3] + const unique = []; + array.forEach((item) => { + if (!unique.includes(item)) { + unique.push(item); + } + }); + return unique } module.exports = dedupe; \ No newline at end of file From 0d3fb031f89a495fa7e359980c0680c55f96b1d8 Mon Sep 17 00:00:00 2001 From: Alex Jamshidi Date: Tue, 12 May 2026 23:20:25 +0100 Subject: [PATCH 4/7] added solutions to max.js --- Sprint-1/implement/max.js | 5 +++- Sprint-1/implement/max.test.js | 42 +++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..77a27adb0 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,7 @@ -function findMax(elements) { +function findMax(list) { + const numbers = list.filter(n => Number.isFinite(n)); + if (numbers.length == 0) { return "-Infinity"} + else {return Math.max(...numbers)}; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..abc39b2f8 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -12,32 +12,72 @@ We have set things up already so that this file can see your function from the o const findMax = require("./max.js"); +describe("calculateMedian", () => { // Given an empty array // 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"); + + it("empty array returns -Infinity", () => { + const list = []; + expect(findMax(list)).toEqual("-Infinity"); + }); // Given an array with one number // When passed to the max function // Then it should return that number + it("array with one number returns that number", () => { + const list = [2]; + expect(findMax(list)).toEqual(2); + }); + // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall + it("array with +ve and -ve numbers, returns largest number overall", () => { + const list = [10, -15]; + expect(findMax(list)).toEqual(10); + }); + + // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero + it("array -ve numbers, returns closest to zero", () => { + const list = [-1, -2, -3, -15]; + expect(findMax(list)).toEqual(-1); + }); + + // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number + it("array with decimal numbers should return largest decimal number", () => { + const list = [1.9, 2.8999, 3.1]; + expect(findMax(list)).toEqual(3.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 + it("array with non-number values, returns the max and ignore non-numeric values", () => { + const list = [1, "a", 2, "b", 3, "c"]; + expect(findMax(list)).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 + + it("array with only non-number values, return the least surprising value given how it behaves for all other inputs", () => { + const list = ["a", "b", "c"]; + expect(findMax(list)).toEqual("-Infinity"); + }); +}); \ No newline at end of file From 357426d3ad6f3d7c4c8014eca21420268a7e5b2a Mon Sep 17 00:00:00 2001 From: Alex Jamshidi Date: Tue, 12 May 2026 23:29:03 +0100 Subject: [PATCH 5/7] tests added for sum.test.js in implement --- Sprint-1/implement/sum.js | 3 ++- Sprint-1/implement/sum.test.js | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..1f8d19de6 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,5 @@ -function sum(elements) { +function sum(list) { + return 0 } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..d16782e64 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -10,27 +10,56 @@ const sum = require("./sum.js"); // Acceptance Criteria: +describe("sum", () => { // Given an empty array // When passed to the sum function // Then it should return 0 -test.todo("given an empty array, returns 0") + it("empty array returns 0", () => { + const list = []; + expect(sum(list)).toEqual(0); + }); // Given an array with just one number // When passed to the sum function // Then it should return that number + it("array with one number returns number", () => { + const list = [1]; + expect(sum(list)).toEqual(1); + }); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum + it("array with negative numbers returns correct sum", () => { + const list = [-1, -2, -3]; + expect(sum(list)).toEqual(-6); + }); + // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum + it("array with decimal numbers returns correct sum", () => { + const list = [1.1, 2.2, 3.3]; + expect(sum(list)).toEqual(6.6); + }); + // 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 + it("array with non-number values returns sum of numerical values only", () => { + const list = [1]; + expect(sum(list)).toEqual(1); + }); + // 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 + + it("array with non-number values only returns least surprising output", () => { + const list = [1]; + expect(sum(list)).toEqual(0); + }); +}); \ No newline at end of file From ee7e6d499dcd6209ad2cf79fb61dd3caaeb2ecf6 Mon Sep 17 00:00:00 2001 From: Alex Jamshidi Date: Tue, 12 May 2026 23:34:46 +0100 Subject: [PATCH 6/7] added solutions to sum.js in implement --- Sprint-1/implement/sum.js | 5 ++++- Sprint-1/implement/sum.test.js | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 1f8d19de6..5c909fc91 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,5 +1,8 @@ function sum(list) { - return 0 + const numbers = list.filter(n => Number.isFinite(n)); + let total = 0; + for (n of numbers) {total += n}; + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index d16782e64..806b8695d 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -50,8 +50,8 @@ describe("sum", () => { // Then it should ignore the non-numerical values and return the sum of the numerical elements it("array with non-number values returns sum of numerical values only", () => { - const list = [1]; - expect(sum(list)).toEqual(1); + const list = [1, 2, "apple", 4, "banana", 3]; + expect(sum(list)).toEqual(10); }); // Given an array with only non-number values @@ -59,7 +59,7 @@ describe("sum", () => { // Then it should return the least surprising value given how it behaves for all other inputs it("array with non-number values only returns least surprising output", () => { - const list = [1]; + const list = ["a", "b", "c"]; expect(sum(list)).toEqual(0); }); }); \ No newline at end of file From 4e0a556f0b913b6a148ff1ada875ec9519194c59 Mon Sep 17 00:00:00 2001 From: Alex Jamshidi Date: Tue, 12 May 2026 23:44:48 +0100 Subject: [PATCH 7/7] changed includes.js code to add for...of loop --- Sprint-1/refactor/includes.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..b779c8586 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,6 +1,15 @@ // Refactor the implementation of includes to use a for...of loop function includes(list, target) { +for (item of list) {if (item === target) {return true;}} + return false; +} + +module.exports = includes; + + +/* Old code: + for (let index = 0; index < list.length; index++) { const element = list[index]; if (element === target) { @@ -8,6 +17,4 @@ function includes(list, target) { } } return false; -} - -module.exports = includes; +*/