diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..a168bc604 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,10 +5,36 @@ // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) // or 'list' has mixed values (the function is expected to sort only numbers). +// Fix this implementation +// Start by running the tests for this function +// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory + +// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) +// or 'list' has mixed values (the function is expected to sort only numbers). +// Fix this implementation +// Start by running the tests for this function +// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory + +// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) +// 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 < 2) { + return null; + } + const filteredList = list.filter((value) => typeof value === "number"); + if (filteredList.length < 2) { + return null; + } + const sortedList = filteredList.sort((a, b) => a - b); + const evenLength = sortedList.length % 2 === 0; + const middleIndex = Math.floor(sortedList.length / 2); + if (evenLength) { + const evenListCalc = + (sortedList[middleIndex] + sortedList[middleIndex - 1]) / 2; + return evenListCalc; + } else { + return sortedList[middleIndex]; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..80a42daaa 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(arr) { + let newArr = []; + for (let i = 0; i < arr.length; i++) { + if (!newArr.includes(arr[i])) { + newArr.push(arr[i]); + } + } + return newArr; +} + +console.log(dedupe([1,2,2,2,4])) +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..3a1813b56 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,22 @@ 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("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, it returns a copy of the original array", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); +}); // 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 // first occurrence of each element from the original array. +test("given an array of strings or numbers, it returns a new array with no duplicates removed", () => { + 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]); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..f6110284b 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,12 @@ function findMax(elements) { + if (elements.length < 1) { + return -Infinity; + } else { + const filteredList = elements.filter((value) => typeof value === "number"); + return Math.max( ...filteredList)} + } module.exports = findMax; + +//console.log(findMax([30, 50, 10, 40])); // 50 diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..96a2b71eb 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,59 @@ 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 +describe("findMax", () => { + [{ input: [3], expected: 3 }].forEach(({ input, expected }) => + it(`returns the only number in the array for [${input}]`, () => + expect(findMax(input)).toEqual(expected)) + ); +}); // 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", () => { + expect(findMax([1, -2, 3, -4, 5])).toEqual(5); +}); // 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 closest to zero", () => { + expect(findMax([-1, -2, -3, -4])).toEqual(-1); +}); // 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 largest decimal number", () => { + expect(findMax([1.5, 1.6, 1.7, 1.8])).toEqual(1.8); + expect(findMax([-1.5, -1.6, -1.7, -1.8])).toEqual(-1.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([1, 2, "3", null, undefined, 4])).toEqual(4); + expect(findMax(["apple", 1, 2, 3, "banana", 4])).toEqual(4); + expect(findMax([1, "2", 3, "4", 5])).toEqual(5); + expect(findMax([1, "apple", 2, null, 3, undefined, 4])).toEqual(4); + expect(findMax([3, "apple", 1, null, 2, undefined, 4])).toEqual(4); + expect(findMax(["banana", 5, 3, "apple", 1, 4, 2])).toEqual(5); +}); // 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", null, undefined])).toEqual(-Infinity); + expect(findMax([null, undefined])).toEqual(-Infinity); + expect(findMax(["apple", "banana"])).toEqual(-Infinity); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..abbbf78f0 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,10 @@ function sum(elements) { + let innit = 0; + const filteredList = elements.filter((value) => typeof value === "number"); + for (let i = 0; i < filteredList.length; i++) { + innit += filteredList[i]; + } +return innit; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..829462dfb 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,44 @@ 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([2])).toEqual(2); +}); // 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([-1, -2, -3])).toEqual(-6); +}); // 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.5])).toEqual(7.5); + expect(sum([-1.5, -2.5, -3.5])).toEqual(-7.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, ignores the non-numerical values and returns the sum of the numerical elements", () => { + expect(sum([1, 2, "3", null, undefined, 4])).toEqual(7); + expect(sum(["apple", 1, 2, 3, "banana", 4])).toEqual(10); + expect(sum([1, "2", 3, "4", 5])).toEqual(9); +}); // 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", null, undefined])).toEqual(0); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..37386436a 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 (let i of list) { + if (i === target) { return true; } }