diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..a3dc44969 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; + if (!Array.isArray(list) || list.length === 0) { + return null; + } + + const filteredNumericArr = list.filter( + (el) => typeof el === "number" && Number.isFinite(el) + ); + + if (filteredNumericArr.length === 0) { + return null; + } + + const sortedArr = [...filteredNumericArr].sort((a, b) => a - b); + const middleIndex = Math.floor(sortedArr.length / 2); + + if (sortedArr.length % 2 !== 0) { + return sortedArr[middleIndex]; + } else { + return (sortedArr[middleIndex - 1] + sortedArr[middleIndex]) / 2; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..66443c5f9 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,15 @@ -function dedupe() {} +function dedupe(arr) { + if (arr.length === 0) return []; + + const noDupsArr = []; + const seenItems = new Set(); + + for (const el of arr) { + if (!seenItems.has(el)) { + noDupsArr.push(el); + seenItems.add(el); + } + } + return noDupsArr; +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..d6f2f47a7 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -12,16 +12,27 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] */ // Acceptance Criteria: +describe("dedupe", () => { + // Given an empty array + // When passed to the dedupe function + // Then it should return an empty array + test("given an empty array, returns an empty array", () => { + expect(dedupe([])).toEqual([]); + }); -// 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"); + // 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", () => { + const input = [1, 65, 2, 298, 3, 729]; + expect(dedupe(input)).toEqual([1, 65, 2, 298, 3, 729]); + }); -// Given an array with no duplicates -// When passed to the dedupe function -// Then it should return a copy of the original array - -// 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 + // 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 duplicate values preserving the first occurrence", () => { + const input = ["a", "b", "a", "c", "b", "d", "e", "d", "c", "a", "b"]; + expect(dedupe(input)).toEqual(["a", "b", "c", "d", "e"]); + }); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..a7538cc73 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,16 @@ function findMax(elements) { + if (elements.length === 0) { + return -Infinity; + } else if (elements.length === 1 && typeof elements[0] === "number") { + return elements[0]; + } + const maxEl = Math.max( + ...elements.filter((el) => typeof el === "number" && !isNaN(el)) + ); + + if (elements.length > 1) { + return maxEl; + } } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..35ff06db9 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -12,32 +12,55 @@ We have set things up already so that this file can see your function from the o const findMax = require("./max.js"); -// Given an empty array -// 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"); - -// Given an array with one number -// When passed to the max function -// Then it should return that number - -// Given an array with both positive and negative numbers -// When passed to the max function -// Then it should return the largest number overall - -// Given an array with just negative numbers -// When passed to the max function -// Then it should return the closest one to zero - -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number - -// 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 -// When passed to the max function -// Then it should return the least surprising value given how it behaves for all other inputs +describe("findMax", () => { + // Given an empty array + // When passed to the max function + // Then it should return -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([15])).toBe(15); + }); + + // 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", () => { + expect(findMax([-112, 7, 3, 20, -2])).toBe(20); + }); + + // 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 largest number - the closest one to zero", () => { + expect(findMax([-653, -80, -7, -4, -12])).toBe(-4); + }); + + // 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([27.3, 654.1, 45.4, 325.9])).toBe(654.1); + }); + + // 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([7, {}, "Greetings!", 34, null, 15.3, undefined, 30, 3878, NaN]) + ).toBe(3878); + }); + + // 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([{}, "Hello!", null, undefined, NaN])).toBe(-Infinity); + }); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..1651d36c8 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,19 @@ function sum(elements) { + const numbers = elements.filter( + (item) => typeof item === "number" && !isNaN(item) + ); + + if (numbers.length === 0) return 0; + + const maxDecimalPlaces = numbers.reduce( + (currMax, el) => Math.max(currMax, (String(el).split(".") || "").length), + 0 + ); + const decMultiplier = 10 ** maxDecimalPlaces; + + const total = numbers.reduce((acc, el) => acc + el, 0); + + return Math.round((total + Number.EPSILON) * decMultiplier) / decMultiplier; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..cb5811d6c 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -9,28 +9,46 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical const sum = require("./sum.js"); // Acceptance Criteria: - -// Given an empty array -// When passed to the sum function -// Then it should return 0 -test.todo("given an empty array, returns 0") - -// Given an array with just one number -// When passed to the sum function -// Then it should return that number - -// Given an array containing negative numbers -// When passed to the sum function -// Then it should still return the correct total sum - -// Given an array with decimal/float numbers -// When passed to the sum function -// Then it should return the correct total sum - -// 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 - -// 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 +describe("sum", () => { + // Given an empty array + // When passed to the sum function + // Then it should return 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 one number, returns that number", () => { + expect(sum([15])).toBe(15); + }); + + // 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 negative numbers, returns the correct sum", () => { + expect(sum([10, -6, 18, -10])).toBe(12); + }); + + // 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([65.5, 8.45, 32.4, 97.36, 10])).toBe(213.71); + }); + + // 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 non-number values, ignores them and returns the sum of numerical elements", () => { + expect(sum([10, "hello", 25, null, 5, undefined, {}, 15])).toBe(55); + }); + + // 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(["Great!", "hi", null, undefined, {}, []])).toBe(0); + }); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..01d7a4cfc 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,9 +1,8 @@ // 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]; - if (element === target) { + for (const el of list) { + if (el === target) { return true; } }