From dc5a3fbdcce8efa1e100e3ccb21ae878da35af45 Mon Sep 17 00:00:00 2001 From: HilolaRustamova Date: Sun, 6 Jul 2025 23:17:53 +0100 Subject: [PATCH 1/6] fixed the code for median --- Sprint-1/fix/median.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..eaff0c97b 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,19 @@ // 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; + //Keep only the numbers + const numbers = list.filter(item => typeof item === 'number'); + // If there are no numbers, return null + if (numbers.length === 0) return null; + // Sort the numbers in ascending order + numbers.sort((a, b) => a - b); + // If the list has an even number of elements, return the average of the two middle + const middleIndex = Math.floor(numbers.length / 2); + if (numbers.length % 2 === 0) { + return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; + } else //If the count is odd, return the middle number + return numbers[middleIndex]; } module.exports = calculateMedian; From 99686a5a5da270c763873b0860f1ebb08e89753e Mon Sep 17 00:00:00 2001 From: HilolaRustamova Date: Tue, 8 Jul 2025 14:02:00 +0100 Subject: [PATCH 2/6] dedup function made and tested --- Sprint-1/implement/dedupe.js | 16 +++++++++++++++- Sprint-1/implement/dedupe.test.js | 13 ++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..b9c235b27 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,15 @@ -function dedupe() {} +function dedupe(arr) { + const seen = new Set(); + const result = []; + + for (const item of arr) { + if (!seen.has(item)) { + seen.add(item); + result.push(item); + } + } + + return result; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..1b47ef311 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -17,11 +17,22 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] // 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, returns a copy of the original array", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + expect(dedupe(['a', 'b', 'c'])).toEqual(['a', 'b', 'c']); +}); // 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("given an array with duplicates, removes duplicates preserving first occurrence", () => { + expect(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']); + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); From 1fcedb752c75f85278deaf7bd35844ad9bfc3fba Mon Sep 17 00:00:00 2001 From: HilolaRustamova Date: Tue, 8 Jul 2025 15:21:09 +0100 Subject: [PATCH 3/6] Find max number test made and tested --- Sprint-1/implement/max.js | 12 +++++++++++- Sprint-1/implement/max.test.js | 24 ++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..da2bd692a 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,14 @@ -function findMax(elements) { +function findMax(arr) { + // Keep only the number values + const numbers = arr.filter(item => typeof item === 'number'); + + // If no numbers found, return -Infinity + if (numbers.length === 0) { + return -Infinity; + } + + // Return the biggest number + 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..ede42fa9b 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,40 @@ 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([])).toBe(-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 that number", () => { + expect(findMax([45])).toBe(45); +}); // 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 overall", + () => {expect(findMax([20,-10,35,-45])).toBe(35) +}); // 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 just negative numbers return the closest one to zero", + () => {expect(findMax([-20,-10,-35,-45])).toBe(-10)}); // 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 decimal numbers return the largest decimal number", + () => {expect(findMax([1.8,2.4,3.5,2.2])).toBe(3.5) }); // 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 +test("given an array with non number values return the max and ignore non-numeric values", + () => {expect(findMax(["Hallo", 5, "Then", 20])).toBe(20)}); +/// 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 return the least surprising value given how it behaves for all other inputs", + () => {expect(findMax(["Hallo", "Lola", "Event", "party"])).toBe(-Infinity)}); From d5df75ff7109f8e455a30c68fd8c22718aa3e861 Mon Sep 17 00:00:00 2001 From: HilolaRustamova Date: Tue, 8 Jul 2025 15:37:50 +0100 Subject: [PATCH 4/6] summing function is made and tested --- Sprint-1/implement/sum.js | 5 ++++- Sprint-1/implement/sum.test.js | 22 ++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..45fff8880 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,7 @@ -function sum(elements) { +function sum(arr) { + return arr + .filter(item => typeof item === "number") // keep only numbers + .reduce((acc, curr) => acc + curr, 0); // sum them up starting from 0 } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..1dcda33ac 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,38 @@ 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([])).toBe(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, returns that number", () => { + expect(sum([30])).toBe(30); +}); // 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, return the correct total sum", () => { + expect(sum([60,-10,-25,30])).toBe(55); +}); // 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, return the correct total sum", () => { + expect(sum([22.5,15,40,3,5])).toBe(85.5); +}); // 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 containing non-number values, return the sum of the numerical elements", () => { + expect(sum(["Enter",30,"hi", 12])).toBe(42); +}); // 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 with only non-number values, return the least surprising value given how it behaves for all other inputs", () => { + expect(sum(["nana","hey","welcome"])).toBe(0); +}); \ No newline at end of file From 5ab93c93fb07d7652af0ab7c0190a3b1f63100d8 Mon Sep 17 00:00:00 2001 From: HilolaRustamova Date: Wed, 9 Jul 2025 13:07:14 +0100 Subject: [PATCH 5/6] refactored version of includes tested and fixed --- Sprint-1/refactor/includes.js | 3 +-- Sprint-1/refactor/includes.test.js | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..cc7fe5bb0 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; } diff --git a/Sprint-1/refactor/includes.test.js b/Sprint-1/refactor/includes.test.js index 812158470..eb5558e97 100644 --- a/Sprint-1/refactor/includes.test.js +++ b/Sprint-1/refactor/includes.test.js @@ -24,7 +24,7 @@ test("returns true when the target is in array multiple times", () => { }); test("returns false for empty array", () => { - const currentOutput = includes([]); + const currentOutput = includes([], "anything"); const targetOutput = false; expect(currentOutput).toEqual(targetOutput); From 83e41d9f81c4b7b5c0dcc1f3cd5637b211c4376f Mon Sep 17 00:00:00 2001 From: HilolaRustamova Date: Wed, 9 Jul 2025 13:50:27 +0100 Subject: [PATCH 6/6] CalculateFrequency function made and tested --- Sprint-1/stretch/aoc-2018-day1/input.js | 17 +++++++++ Sprint-1/stretch/aoc-2018-day1/input.test.js | 38 ++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 Sprint-1/stretch/aoc-2018-day1/input.js create mode 100644 Sprint-1/stretch/aoc-2018-day1/input.test.js diff --git a/Sprint-1/stretch/aoc-2018-day1/input.js b/Sprint-1/stretch/aoc-2018-day1/input.js new file mode 100644 index 000000000..d89c1eed3 --- /dev/null +++ b/Sprint-1/stretch/aoc-2018-day1/input.js @@ -0,0 +1,17 @@ + + + +function calculateFrequency(input) { + let frequency = 0; + + // Convert the input string into an array of changes + const changes = input.trim().split('\n'); + + for (let change of changes) { + frequency += Number(change); // Convert each string to number and add + } + + + return frequency; +} +module.exports = calculateFrequency; \ No newline at end of file diff --git a/Sprint-1/stretch/aoc-2018-day1/input.test.js b/Sprint-1/stretch/aoc-2018-day1/input.test.js new file mode 100644 index 000000000..94b715eae --- /dev/null +++ b/Sprint-1/stretch/aoc-2018-day1/input.test.js @@ -0,0 +1,38 @@ +const calculateFrequency = require("./input.js"); + + +test('example from prompt', () => { + // Input as a multiline string (mimics what the puzzle provides) + const input = `+1\n-2\n+3\n+1`; + // Expected result: 0 + 1 - 2 + 3 + 1 = 3 + expect(calculateFrequency(input)).toBe(3); +}); + // All positive values +test('all positive numbers', () => { + const input = `+1\n+1\n+1`; + // Expected result: 0 + 1 + 1 + 1 = 3 + expect(calculateFrequency(input)).toBe(3); +}); +// All negative values +test('all negative numbers', () => { + const input = `-1\n-2\n-3`; + // Expected result: 0 - 1 - 2 - 3 = -6 + expect(calculateFrequency(input)).toBe(-6); +}); +// Mix of positive and negative that cancels out +test('mixed values resulting in zero', () => { + const input = `+1\n+1\n-2`; + + // Expected result: 0 + 1 + 1 - 2 = 0 + expect(calculateFrequency(input)).toBe(0); +}); + +// Empty input string should return 0 +test('empty input returns 0', () => { + const input = ``; + + // No changes, frequency stays at 0 + expect(calculateFrequency(input)).toBe(0); +}); + +