diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..ade66b21c 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; + if (!Array.isArray(list)) { + return null; + } + + const numericList = list.filter((item) => Number.isFinite(item)); + + if (numericList.length === 0) { + return null; + } + + numericList.sort((a, b) => a - b); + + const middleIndex = Math.floor(numericList.length / 2); + + if (numericList.length % 2 === 0) { + return (numericList[middleIndex - 1] + numericList[middleIndex]) / 2; + } else { + return numericList[middleIndex]; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..f03270f41 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,19 @@ -function dedupe() {} +function dedupe(input) { + if (!Array.isArray(input)) { + return null; + } + + const newArray = []; + + for (let i = 0; i < input.length; i++) { + const element = input[i]; + + if (!newArray.includes(element)) { + newArray.push(element); + } + } + + return newArray; +} + +module.exports = dedupe; \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..2c7e7e35b 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,32 @@ 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([]); + expect(result).not.toBe(input); +}); // 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 a copy of the original array", () => { + const input = [1, 2, 3]; + const result = dedupe(input); + expect(result).toEqual([1, 2, 3]); + expect(result).not.toBe(input); +}); // 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("removes duplicate values, preserving the 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]); +}); + +test("returns null for non-array inputs", () => { + expect(dedupe("not an array")).toBeNull(); + expect(dedupe(123)).toBeNull(); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..1393933cb 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,15 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + return null; + } + + const numericElements = elements.filter((item) => typeof item === "number"); + + if (numericElements.length === 0) { + return -Infinity; + } + + return Math.max(...numericElements); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..1f3aeca3d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,53 @@ 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([])).toEqual(-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([42])).toEqual(42); +}); // 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([-10, 5, 100, -50, 20])).toEqual(100); +}); // 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, returns the closest one to zero", () => { + expect(findMax([-10, -20, -3, -4])).toEqual(-3); +}); // 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, returns the largest decimal number", () => { + expect(findMax([1.5, 10.5, 0.5, 10.4])).toEqual(10.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 - +test("given an array with non-number values, returns the max and ignores non-numeric values", () => { + expect(findMax(["apple", "banana", 10, "300", 20])).toEqual(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, returns -Infinity", () => { + expect(findMax(["apple", "banana", "cherry"])).toEqual(-Infinity); +}); + +//test NaN +test("NaN", () => { + expect(findMax([NaN])).toEqual(NaN); +}); + +//test with variables +test("variables", () => { + expect(findMax([0, NaN, 1])).toEqual(NaN); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..d8d874edb 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,17 @@ function sum(elements) { + if (!Array.isArray(elements)) { + return null; + } + + let total = 0; + + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number") { + total += elements[i]; + } + } + + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..fe889eec1 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,47 @@ 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([])).toEqual(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([42])).toEqual(42); +}); // 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, returns the correct total sum", () => { + expect(sum([10, -20, 5])).toEqual(-5); +}); // 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, returns the correct total sum", () => { + expect(sum([1.5, 2.5, 3.1])).toBeCloseTo(7.1); +}); // 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, returns the sum of the numerical elements", () => { + expect(sum([10, "apple", 20, null, 30])).toEqual(60); +}); // 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, returns 0", () => { + expect(sum(["apple", "banana", "cherry"])).toEqual(0); +}); + +//Test NaN +test("NaN", () => { + expect(sum([NaN, 1])).toEqual(NaN); +}); + +//Test Infinity +test("Infinity", () => { + expect(sum([Infinity, -Infinity])).toEqual(NaN); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..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++) { - const element = list[index]; + for (const element of list) { if (element === target) { return true; }