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; 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]); +}); 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)}); 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 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); 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); +}); + +