diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..13ca3cd83 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,28 @@ // 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; + // Step 1: must be an array + if (!Array.isArray(list)) return null; + + // Step 2: filter numeric values + const numericValues = list.filter( + (item) => typeof item === "number" && !Number.isNaN(item) + ); + + // Step 3: must have at least one number + if (numericValues.length === 0) return null; + + // Step 4: sort safely + const sortedList = numericValues.slice().sort((a, b) => a - b); + + const middleIndex = Math.floor(sortedList.length / 2); + + // Step 5: median + if (sortedList.length % 2 !== 0) { + return sortedList[middleIndex]; + } + + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..08648ac3a 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(elements) { + if (elements.length === 0) return []; + return elements.filter((item, index) => elements.indexOf(item) === index); +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..363636a58 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,31 @@ E.g. dedupe([1, 2, 1]) target output: [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"); +test("given an empty array, it returns an empty array", () => { + const input = []; + const result = dedupe(input); + expect(result).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, it returns return a copy of the original array", () => { + const input = [1, 2, 3, 4, 5]; + const result = dedupe(input); + expect(result).toEqual([1, 2, 3, 4, 5]); +}); // 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 +// Then it should remove the duplicate values, preserving the first occurrence of each element +test("given an array with strings, it removes the duplicate, preserving first occurrence of each element", () => { + const input = ["a", "a", "a", "b", "b", "c"]; + const result = dedupe(input); + expect(result).toEqual(["a", "b", "c"]); +}); +test("given an array with numbers it removes the duplicate, preserving first occurrence of each element", () => { + const input = [5, 1, 1, 2, 3, 2, 5, 8]; + const result = dedupe(input); + expect(result).toEqual([5, 1, 2, 3, 8]); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..5088fb0b7 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,10 @@ function findMax(elements) { + if (!Array.isArray(elements) || elements.length === 0) return -Infinity; + return Math.max( + ...elements.filter( + (item) => typeof item === "number" && !Number.isNaN(item) + ) + ); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..d0af145a1 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,62 @@ 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", () => { + const input = [] + const result = findMax(input) + expect(result).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, its should return that number', () => { + const input = [5] + const result = findMax(input) + expect(result).toBe(5); +}); // 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, it should return largest overall', () => { + const input = [5, -5, -10, -1, 6, 8, 10] + const result = findMax(input) + expect(result).toBe(10); +}); // 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, it should return the closest one to zero', () => { + const input = [-10, -5, -2, -8]; + const result = findMax(input); + expect(result).toBe(-2); +}) // 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 just decimal numbers, it should return the largest decimal number", () => { + const input = [1.0, 2.5, 2.2, 5.8]; + const result = findMax(input); + expect(result).toBe(5.8); +}); // Given an array with non-number values // When passed to the max function // Then it should return the max and ignore non-numeric values +test("given an array with non-number value, it should return the max and ignore non-numeric value", () => { + const input = [4, 5, 'arr', 2, []," ", 15, 8]; + const result = findMax(input); + expect(result).toBe(15); +}); // 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 value, it should return the least surprising value given how it behaves for all other inputs", () => { + const input = [true, {}, "arr", null, [], " ", NaN]; + const result = findMax(input); + expect(result).toBe(-Infinity); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..542e623ea 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,8 @@ function sum(elements) { + if (!Array.isArray(elements) || elements.length === 0) return 0; + return elements + .filter((item) => typeof item === "number" && !Number.isNaN(item)) + .reduce((acc, n) => acc + n, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..317eaebe6 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,53 @@ 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", () => { + const input = []; + const result = sum(input); + expect(result).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, it should return that number", () => { + const input = [5]; + const result = sum(input); + expect(result).toBe(5); +}); // 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, it should return the correct total sum", () => { + const input = [5, -6, 10, -12]; + const result = sum(input); + expect(result).toBe(-3); +}); // 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, it should return the correct total sum", () => { + const input = [5, 6.6, 10, 5.12]; + const result = sum(input); + expect(result).toBe(26.720000000000002); +}); // 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, it should ignore the numerical values and return sum of numeric element", () => { + const input = [4, 5, "arr", 2, [], " ", 15, 8]; + const result = sum(input); + expect(result).toBe(34); +}); // 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, it should return least surprising value given how it behaves for all other inputs", () => { + const input = [true, {}, "arr", null, [], " ", NaN]; + const result = sum(input); + expect(result).toBe(0); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..ecc97ff3d 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; } @@ -11,3 +10,10 @@ function includes(list, target) { } module.exports = includes; + +// const array = ["a", "b", "c"]; + +// for (const element of array) { +// console.log(element); +// } for (variable of iterable) statement +