From dec9c100ad19d722fd7aa610b10806736074a816 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Fri, 10 Jul 2026 16:56:48 +0100 Subject: [PATCH 01/12] fix calculateMedian function to pass the tests --- Sprint-1/fix/median.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..16c7d6a49 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,25 @@ // 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; + // Check if the input is an array + if (!Array.isArray(list)) { + return null; + } + // Filter out non-numeric values from the array + const numbers = list.filter((x) => typeof x === "number"); + // If the filtered array is empty, return null + if (numbers.length === 0) { + return null; + } + + // Sort the numbers in ascending order so the median can be calculated correctly + numbers.sort((a, b) => a - b); + + const middleIndex = Math.floor(numbers.length / 2); + // Calculate the median based on whether the length of the array is even or odd + return numbers.length % 2 === 0 + ? (numbers[middleIndex - 1] + numbers[middleIndex]) / 2 + : numbers[middleIndex]; } module.exports = calculateMedian; From 777a9a43edcb5bc721b40d64470ad174ec9cd3c1 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Mon, 13 Jul 2026 21:44:06 +0100 Subject: [PATCH 02/12] add remove duplicate elements function --- Sprint-1/implement/dedupe.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..4ff2bab3b 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(inputArray) { + const uniqueElements = []; + for (const el of inputArray) { + if (uniqueElements.includes(el)) { + continue; + } + uniqueElements.push(el); + } + return uniqueElements; +} + +module.exports = dedupe; From 4b21149a232aad69ad654b5677d46d290554217c Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Mon, 13 Jul 2026 21:44:22 +0100 Subject: [PATCH 03/12] add tests for dedupe function --- Sprint-1/implement/dedupe.test.js | 40 +++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..501781aa5 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -13,16 +13,32 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] // Acceptance Criteria: -// 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", () => { + // Given an empty array + // When passed to the dedupe function + // Then it should return an empty array + it("returns an empty array when given empty array", () => { + expect(dedupe([])).toStrictEqual([]); + }); -// Given an array with no duplicates -// When passed to the dedupe function -// Then it should return a copy of the original 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 -// first occurrence of each element from the original array. + // Given an array with no duplicates + // When passed to the dedupe function + // Then it should return a copy of the original array + it("returns a copy of the array is the one given without duplicates", () => { + expect(dedupe([1, 2, 3, 4])).toStrictEqual([1, 2, 3, 4]); + expect(dedupe(["a", "b", "c", "d"])).toStrictEqual(["a", "b", "c", "d"]); + }); + // 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. + it("returns an array of unique elements with order preserving the first occurence of each element", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toStrictEqual([ + "a", + "b", + "c", + ]); + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toStrictEqual([5, 1, 2, 3, 8]); + expect(dedupe([1, 2, 1])).toStrictEqual([1, 2]); + }); +}); From ca97ed235c9999dc4059c34e1917e35b6481c866 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Tue, 14 Jul 2026 14:28:45 +0100 Subject: [PATCH 04/12] add findMax function solution --- Sprint-1/implement/max.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..4037a3606 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,17 @@ function findMax(elements) { + // filter out only numbers to the new array + const numElements = elements.filter( + (el) => !isNaN(el) && typeof el === "number" + ); + + return numElements.reduce( + (acc, curr) => (acc > curr ? acc : curr), + -Infinity + ); } module.exports = findMax; + +console.log(findMax([-10, 20, 0, 100, 1])); +console.log(findMax([])); +console.log(findMax(["I", 2, "am", -1000, "Lord", "200", "Voldemort", 10])); From 8e045f897e72cb0edac26c25810f363f462a684e Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Tue, 14 Jul 2026 14:29:01 +0100 Subject: [PATCH 05/12] add findMax function tests --- Sprint-1/implement/max.test.js | 84 ++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..ba27ac340 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -1,43 +1,67 @@ -/* Find the maximum element of an array of numbers +/* find the maximum element of an array of numbers -In this kata, you will need to implement a function that find the largest numerical element of an array. +in this kata, you will need to implement a function that find the largest numerical element of an array. -E.g. max([30, 50, 10, 40]), target output: 50 -E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) +e.g. max([30, 50, 10, 40]), target output: 50 +e.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) -You should implement this function in max.js, and add tests for it in this file. +you should implement this function in max.js, and add tests for it in this file. -We have set things up already so that this file can see your function from the other file. +we have set things up already so that this file can see your function from the other file. */ const findMax = require("./max.js"); -// 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"); +describe("findMax function", () => { + // 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. + it("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 + // given an array with one number + // when passed to the max function + // then it should return that number + it("given an array with one number, returns that number", () => { + expect(findMax([10])).toEqual(10); + }); -// Given an array with both positive and negative numbers -// When passed to the max function -// Then it should return the largest number overall + // given an array with both positive and negative numbers + // when passed to the max function + // then it should return the largest number overall + it("give an array with both positive and negative numbers, returns larges number overall", () => { + expect(findMax([-10, 20, 0, 100, 1])).toEqual(100); + expect(findMax([-10, -100, 0, 11, 1, -5])).toEqual(11); + }); -// Given an array with just negative numbers -// When passed to the max function -// Then it should return the closest one to zero + // given an array with just negative numbers + // when passed to the max function + // then it should return the closest one to zero + it("given an array with just negative numbers, returns number closest to zero", () => { + expect(findMax([-1, -200, -35, -4, -100])).toEqual(-1); + }); + // given an array with decimal numbers + // when passed to the max function + // then it should return the largest decimal number + it("given an array with decimal numbers, returns largest decimal number", () => { + expect(findMax([-1.2, 3.14, -0.1, -0.007, 5.5, 5.50001])).toEqual(5.50001); + }); -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number + // 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("given an array with non-number values, returns max, non-numeric values ignored", () => { + expect( + findMax(["I", 2, "am", -1000, "Lord", "200", "Voldemort", 10]) + ).toEqual(10); + }); -// Given an array with non-number values -// When passed to the max function -// Then it should return the max and ignore non-numeric values - -// 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 + // 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("given an array with only non-numeric values, it returns -Infinity", () => { + expect(findMax(["I", "am", "Lord", "Voldemort"])).toEqual(-Infinity); + }); +}); From 790bca70ef16cd83fc41df51e99e374088454c3d Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Tue, 14 Jul 2026 14:57:00 +0100 Subject: [PATCH 06/12] add sum function solution --- Sprint-1/implement/sum.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..dc426d36d 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,10 @@ function sum(elements) { + return elements.reduce((acc, curr) => { + if (typeof curr !== "number") { + return acc; + } + return acc + curr; + }, 0); } module.exports = sum; From d39dc6310d0553030b9f4ae397c85bdbe8f8bbb4 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Tue, 14 Jul 2026 14:57:06 +0100 Subject: [PATCH 07/12] add sum function tests --- Sprint-1/implement/sum.test.js | 76 ++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..701320f3d 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -7,30 +7,52 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical */ const sum = require("./sum.js"); - -// 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") - -// Given an array with just one number -// When passed to the sum function -// Then it should return that number - -// Given an array containing negative numbers -// When passed to the sum function -// Then it should still return the correct total sum - -// Given an array with decimal/float numbers -// When passed to the sum function -// Then it should return the correct total sum - -// 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 - -// 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 +describe("sum function", () => { + // Acceptance Criteria: + + // Given an empty array + // When passed to the sum function + // Then it should return 0 + it("given an empty array, should return 0", () => { + expect(sum([])).toEqual(0); + }); + + // Given an array with just one number + + // When passed to the sum function + // Then it should return that number + it("given an array of just one number, returns that number", () => { + expect(sum([2])).toEqual(2); + }); + // Given an array containing negative numbers + // When passed to the sum function + // Then it should still return the correct total sum + it("given an array containing negative numbers, returns correct total sum", () => { + expect(sum([-1, -3, -5, 0])).toEqual(-9); + }); + // Given an array with decimal/float numbers + // When passed to the sum function + // Then it should return the correct total sum + it("given an array with decimal numbers, returns correct total sum", () => { + expect(sum([-1.2, 2.3, 0.001, 2.0])).toEqual(3.101); + }); + // 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("given an array containing non-numerical values, returns the sum of the numerical values", () => { + expect(sum(["I", 2, "am", -1000, "Lord", "200", "Voldemort", 10])).toEqual( + -988 + ); + }); + // 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("given an array with only non-numerical values, returns 0", () => { + expect(sum(["I", "am", "Lord", "Voldemort"])).toEqual(0); + }); + + // Additional test: only positive integers + it("given an array with only positive integers, returns the sum of the elements", () => { + expect(sum([1, 2, 3, 100])).toEqual(106); + }); +}); From fcd41ac55a12bb41e5ad4ed224c0625a51fbaf3b Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Tue, 14 Jul 2026 15:07:41 +0100 Subject: [PATCH 08/12] fix: bug const instead of let --- Sprint-1/refactor/includes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..52e7df11f 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -2,7 +2,7 @@ function includes(list, target) { for (let index = 0; index < list.length; index++) { - const element = list[index]; + let element = list[index]; if (element === target) { return true; } From 767a4984145f1eff026131d38ec52484d6be69e2 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Tue, 14 Jul 2026 15:14:59 +0100 Subject: [PATCH 09/12] refactor includes function --- Sprint-1/refactor/includes.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 52e7df11f..8c9ae2e66 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++) { - let element = list[index]; + for (const element of list) { if (element === target) { return true; } From 2cd2b5a4f176d03ca78712e349759270b78ddd68 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Tue, 14 Jul 2026 22:54:40 +0100 Subject: [PATCH 10/12] add input read --- Sprint-1/stretch/aoc-2018-day1/solution.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..fa8371206 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,11 @@ +const fs = require("fs"); + +let inputData; +fs.readFile("./input.txt", "utf8", (err, data) => { + if (err) { + console.error(err); + return; + } + inputData.push(...data.split("\n")); + // console.log(inputData); +}); From ab24307473f8b2dbcec57601888b06ef7f1c67bb Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Tue, 14 Jul 2026 23:03:10 +0100 Subject: [PATCH 11/12] fix reading an cleaning input data --- Sprint-1/stretch/aoc-2018-day1/solution.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index fa8371206..001cd53af 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -1,11 +1,10 @@ const fs = require("fs"); -let inputData; -fs.readFile("./input.txt", "utf8", (err, data) => { - if (err) { - console.error(err); - return; - } - inputData.push(...data.split("\n")); - // console.log(inputData); -}); +let frequencies; +try { + const data = fs.readFileSync("./input.txt", "utf8"); + const inputData = data.split("\n").filter((line) => line.trim() !== ""); + frequencies = inputData.map(Number); +} catch (err) { + console.error("Error reading file:", err); +} From 6474d852611bcbc8c18e9053d92452e45f689d51 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Tue, 14 Jul 2026 23:14:47 +0100 Subject: [PATCH 12/12] add solution to aoc 2018 day1 part1 --- Sprint-1/stretch/aoc-2018-day1/solution.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index 001cd53af..bb2138356 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -1,6 +1,8 @@ const fs = require("fs"); let frequencies; + +// read the input file and convert the input elements into numbers try { const data = fs.readFileSync("./input.txt", "utf8"); const inputData = data.split("\n").filter((line) => line.trim() !== ""); @@ -8,3 +10,8 @@ try { } catch (err) { console.error("Error reading file:", err); } + +function getFrequenciesSum(frequencies) { + return frequencies.reduce((acc, curr) => acc + curr, 0); +} +console.log(getFrequenciesSum(frequencies)); // result is 529