From 8979b2bc3ac1e9b9351a88bef1e2c1e06a057e97 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Thu, 2 Jul 2026 22:32:09 +0100 Subject: [PATCH 1/9] Implement median calculation logic in median.js --- Sprint-1/fix/median.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..3e775bfe6 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,23 @@ // 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 sortedList = list + .filter((element) => typeof element === "number") + .sort((a, b) => a - b); + + if (sortedList.length === 0) { + return null; + } + const middleIndex = Math.floor(sortedList.length / 2); + + if (sortedList.length % 2 === 0) { + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } else { + return sortedList[middleIndex]; + } } module.exports = calculateMedian; From af97145169b4d71dc5eb4ad2e1672403851b1d6b Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 3 Jul 2026 01:46:31 +0100 Subject: [PATCH 2/9] add tests for dedupe function --- Sprint-1/implement/dedupe.js | 8 ++++- Sprint-1/implement/dedupe.test.js | 55 ++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..afa6e6007 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,7 @@ -function dedupe() {} +function dedupe(array) { + if (Array.isArray(array)) { + return []; + } +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..e2ed47e26 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,4 +1,57 @@ const dedupe = require("./dedupe.js"); + +describe("dedupe", () => { + test("Return empty array for given empty array", () => { + expect(dedupe([])).toEqual([]); + }); + + test("Return copy for no duplicates input", () => { + [ + [2, 4, 1, 6.9, 0], + ["a", "d", "e", "b", "n", "c"], + ["2", "4", "1", "6", "9", "0"], + ["cat", "dog", "cow"], + ].forEach((input) => { + expect(dedupe(input)).toEqual(input); + }); + }); + + test("removes duplicate strings", () => { + [ + { input: ["a", "a", "a", "b", "b", "c"], expected: ["a", "b", "c"] }, + { + input: ["2", "4", "1", "6", "2", "4"], + expected: ["2", "4", "1", "6"], + }, + { input: ["cat", "dog", "cat"], expected: ["cat", "dog"] }, + ].forEach((obj) => expect(dedupe(obj.input)).toEqual(obj.expected)); + }); + + test("removes duplicate numbers", () => { + [ + { input: [1, 2, 1], expected: [1, 2] }, + { + input: [2, 4, 1, 6.9, 2, 0], + expected: [2, 4, 1, 6.9, 0], + }, + ].forEach((obj) => expect(dedupe(obj.input)).toEqual(obj.expected)); + }); + + test("removes duplicate numbers and strings", () => { + expect(dedupe([1, "a", 2, "b", 1, "a"])).toEqual([1, "a", 2, "b"]); + }); + + test("does not mutate original array", () => { + const input = [1, 2, 3]; + const original = [...input]; + + const result = dedupe(input); + + expect(result).not.toBe(input); + expect(input).toEqual(original); + }); +}); + /* Dedupe Array @@ -24,5 +77,5 @@ test.todo("given an empty array, it returns an empty array"); // 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 +// Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. From 7c459a8b4d0a4b42478968caa6d59d0beef9670a Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 3 Jul 2026 02:46:53 +0100 Subject: [PATCH 3/9] implement dedupe function and add new Jest test cases --- Sprint-1/implement/dedupe.js | 6 +++--- Sprint-1/implement/dedupe.test.js | 28 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index afa6e6007..5fe58a4b2 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,7 +1,7 @@ function dedupe(array) { - if (Array.isArray(array)) { - return []; - } + if (!Array.isArray(array)) return []; + + return [...new Set(array)]; } module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index e2ed47e26..d9c3467b7 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -38,7 +38,33 @@ describe("dedupe", () => { }); test("removes duplicate numbers and strings", () => { - expect(dedupe([1, "a", 2, "b", 1, "a"])).toEqual([1, "a", 2, "b"]); + expect(dedupe([1, "a", 2, "b", 1, "a", "2"])).toEqual([ + 1, + "a", + 2, + "b", + "2", + ]); + }); + + test("handles special characters", () => { + expect(dedupe(["@", "@", "#", "#", "!"])).toEqual(["@", "#", "!"]); + }); + + test("handles strings with spaces", () => { + expect(dedupe(["a a", "a a", "b b"])).toEqual(["a a", "b b"]); + }); + + test("handles null and undefined", () => { + expect(dedupe([null, null, undefined])).toEqual([null, undefined]); + }); + + test("handles boolean values", () => { + expect(dedupe([true, false, true])).toEqual([true, false]); + }); + + [null, undefined, 123, "abc", {}].forEach((input) => { + expect(dedupe(input)).toEqual([]); }); test("does not mutate original array", () => { From 14bdc7f908a874d5418a86ec9b1b6ecb116ccd9a Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 3 Jul 2026 13:25:27 +0100 Subject: [PATCH 4/9] Refactor the implementation of includes --- Sprint-1/refactor/includes.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..177564d3d 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,6 +1,6 @@ // Refactor the implementation of includes to use a for...of loop -function includes(list, target) { +/*function includes(list, target) { for (let index = 0; index < list.length; index++) { const element = list[index]; if (element === target) { @@ -9,5 +9,14 @@ function includes(list, target) { } return false; } +*/ +function includes(list, target) { + for (const element of list) { + if (element === target) { + return true; + } + } + return false; +} module.exports = includes; From 01047f61da523b2ed52442f16f2b439ca38e334f Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 3 Jul 2026 14:26:41 +0100 Subject: [PATCH 5/9] add Jest tests for max function covering edge cases and mixed inputs --- Sprint-1/implement/max.test.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..e7b77dbf6 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -12,11 +12,40 @@ We have set things up already so that this file can see your function from the o const findMax = require("./max.js"); +describe("findMax", () => { + test("return -Infinity for empty array", () => { + expect(findMax([])).toEqual(-Infinity); + }); + + test("return same number with one value array", () => { + expect(findMax([3])).toEqual(3); + }); + + test("return largest number with both positive and negative ", () => { + expect(findMax([3, 6, -2, 0, -5, 2])).toEqual(3); + }); + + test("return largest number with just negative numbers ", () => { + expect(findMax([-3, -6, -2, -1, -5, -2])).toEqual(-1); + }); + + test("return largest number with just decimal numbers ", () => { + expect(findMax([-3, 6.75, 2, 1, 6.25, 2])).toEqual(6.75); + }); + + test("ignore non-numeric values with non-number values array", () => { + expect(findMax([-3, 6, "a", 1, "abc", 2])).toEqual(6); + }); + + test("return -Infinity with all non-number values", () => { + expect(findMax(["abc", "h", "a", "r", "b"])).toEqual(-Infinity); + }); +}); + // 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"); // Given an array with one number // When passed to the max function From b5ef1f249e9c8281318dc26e4de410d4ae423ecd Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 3 Jul 2026 14:38:15 +0100 Subject: [PATCH 6/9] implement max function --- Sprint-1/implement/max.js | 3 +++ Sprint-1/implement/max.test.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..b6d79cced 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,7 @@ function findMax(elements) { + const numberArray = elements.filter((el) => typeof el === "number"); + if (numberArray.length === 0) return -Infinity; + return Math.max(...numberArray); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index e7b77dbf6..166393196 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -22,7 +22,7 @@ describe("findMax", () => { }); test("return largest number with both positive and negative ", () => { - expect(findMax([3, 6, -2, 0, -5, 2])).toEqual(3); + expect(findMax([3, 6, -2, 0, -5, 2])).toEqual(6); }); test("return largest number with just negative numbers ", () => { From 41f284e7aa819a1b5bbc3378816fd5c42769e627 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 3 Jul 2026 17:15:57 +0100 Subject: [PATCH 7/9] add Jest tests for sum function including edge cases and non-numeric handling --- Sprint-1/implement/sum.test.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..27387b856 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -8,12 +8,38 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical const sum = require("./sum.js"); +describe("sum", () => { + test("Return 0 for empty array", () => { + expect(sum([])).toEqual(0); + }); + + test("Return same number with one value array", () => { + expect(sum([5])).toEqual(5); + }); + + test("Return sum for array containing negative numbers", () => { + expect(sum([2, -5, 0, 9, -10, 15])).toEqual(11); + }); + + test("Return sum for decimal/float numbers array", () => { + expect(sum([2, 5.5, 0, 9, 10.75])).toEqual(27.25); + }); + + test("Ignore non-numeric values with non-number values array", () => { + expect(sum([-3, 6, "a", 1, "abc", 2])).toEqual(6); + }); + + test("return 0 with all non-number values", () => { + expect(sum(["abc", "h", "a", "r", "b"])).toEqual(0); + }); +}); + // Acceptance Criteria: // 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"); // Given an array with just one number // When passed to the sum function From 6a842bce9dcf1fccbe717f810a9ee131b422bdd5 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 3 Jul 2026 17:30:31 +0100 Subject: [PATCH 8/9] implement sum function --- Sprint-1/implement/sum.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..2d9e5101c 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,6 @@ function sum(elements) { + return elements + .filter((el) => typeof el === "number") + .reduce((sum, num) => sum + num, 0); } - module.exports = sum; From 6b6f1fbc327ddf0af804cb837114d5c29f07de68 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 3 Jul 2026 20:00:22 +0100 Subject: [PATCH 9/9] Implement findFinalFrequency solution --- Sprint-1/stretch/aoc-2018-day1/solution.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..681b4952b 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,9 @@ +const fs = require("fs"); +const data = fs.readFileSync("input.txt", "utf-8"); + +function findFinalFrequency(inputString) { + const numberArray = inputString.split("\n").map(Number); + return numberArray.reduce((sum, num) => sum + num, 0); +} + +console.log(findFinalFrequency(data));