diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..cb6c95960 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,26 @@ // 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; + // validation + if (!Array.isArray(list)) return null; + if (list.length === 0) return null; + + // sorted copy for correct median calculation + const sorted = [...list] + .filter((items) => typeof items === "number" && !isNaN(items)) + .sort((a, b) => a - b); + + if (sorted.length === 0) return null; + + let correctMedian; + if (sorted.length % 2 === 0) { + const mid = sorted.length / 2; + correctMedian = (sorted[mid - 1] + sorted[mid]) / 2; + } else { + const mid = Math.floor(sorted.length / 2); + correctMedian = sorted[mid]; + } + return correctMedian; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..2b1f9a379 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(inputArray) { + if (inputArray.length === 0) return inputArray; + let fixArray = []; + for (const item of inputArray) { + if (!fixArray.includes(item)) { + fixArray.push(item); + } + } + return fixArray; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..ce0fefc7e 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,4 +1,5 @@ const dedupe = require("./dedupe.js"); +const findMax = require("./max.js"); /* Dedupe Array @@ -16,13 +17,36 @@ E.g. dedupe([1, 2, 1]) returns [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("with input of empty array should return, empty array", () => { + let emptyArray = []; + expect(dedupe(emptyArray)).toEqual([]); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array +test("with input of array with no duplicates should return a copy of original array", () => { + const normalArray = [1, 2, 3, 4, 5, 6, 10]; + const expected = [1, 2, 3, 4, 5, 6, 10]; // separate copy + + const result = dedupe(normalArray); + + // 1. Correct values + expect(result).toEqual(expected); + + // 2. Different array in memory + expect(result).not.toBe(normalArray); + + // 3. Original array not mutated + expect(normalArray).toEqual(expected); +} +); // 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 +// Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. +test("with input of empty array should return, empty array", () => { + let mixValueArray = [1, 1, 1, 2, 2, "Hello", "Hello", "Hello", 3, 4, 4, 5]; + expect(dedupe(mixValueArray)).toEqual([1, 2, "Hello", 3, 4, 5]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..08d4d2864 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,13 @@ function findMax(elements) { + if (elements.length === 0) return -Infinity; + + const filteredArray = elements.filter( + (item) => typeof item === "number" && !isNaN(item) + ); + if (filteredArray.length === 0) return null; + const sortedArray = filteredArray.sort((a, b) => a - b); + const maxArrayValue = sortedArray[sortedArray.length - 1]; + return maxArrayValue; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..64cf0f64d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,55 @@ 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("when an empty array push into the function it should return empty array", () => { + let emptyArray = []; + expect(findMax(emptyArray)).toEqual(-Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("When an array have only one number value", () => { + let mixArray = ["Hello", "Hi", null, undefined, 5]; + expect(findMax(mixArray)).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("When an array have only one number value", () => { + let mixArray = [-5, 3, 10, -11, -12, -20, 50]; + expect(findMax(mixArray)).toBe(50); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("When an array have only one number value", () => { + let mixArray = [-1, -4, -5, -6 - 2, -3, -10]; + expect(findMax(mixArray)).toBe(-1); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("When an array have decimal number value", () => { + let decimalArray = [2.3, 1.5, 6.5, 10.2, 11.5]; + expect(findMax(decimalArray)).toBe(11.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("When an array have number value mix with non-number value, the return should only be number value", () => { + let decimalArray = ["Hello", "Hi", 1, 12312321n, undefined]; + expect(findMax(decimalArray)).toBe(1); +}); // 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("When an array have only non-number values ", () => { + let nonNumberArray = ["Hello", "Hi", undefined, null, "23"]; + expect(findMax(nonNumberArray)).toBe(null); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..e70c541c2 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,14 @@ function sum(elements) { + if (elements.length === 0) return 0; + let filteredArray = elements.filter( + (items) => typeof items === "number" && !isNaN(items) + ); + if (filteredArray.length === 0) return null; + let sumTotal = 0; + for (let i = 0; i < filteredArray.length; i++) { + sumTotal += filteredArray[i]; + } + return sumTotal; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..1dd9a0eae 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", () => { + let emptyArray = []; + expect(sum(emptyArray)).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 1 number value, returns that number value", () => { + let mixArray = ["1", "Hello", 20, null, undefined]; + expect(sum(mixArray)).toBe(20); +}); // 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 with 1 number value, returns that number value", () => { + let negativeArray = [-1, -20, -30, -50, -60]; + expect(sum(negativeArray)).toBe(-161); +}); // 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 1 number value, returns that number value", () => { + let negativeArray = [1.5, 20.3, 50.43, 6.7, 10.5]; + expect(sum(negativeArray)).toBeCloseTo(89.43,2); +}); // 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 with 1 number value, returns that number value", () => { + let mixedArray2 = ["Hello", 20.17, null, 9.7, undefined]; + expect(sum(mixedArray2)).toBe(29.87); +}); // 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 1 number value, returns that number value", () => { + let nonNumberArray = ["Hello", "2", null, "@", undefined]; + expect(sum(nonNumberArray)).toBe(null); +});