diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..d2b910fe3 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,27 @@ // 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; + list = list || []; + let newOrder = Array.from(list); + let i = 0; + while (i < newOrder.length) { + if (typeof newOrder[i] !== "number") newOrder.splice(i, 1); + else i++; + } + if (newOrder.length === 0) { + return null; + } + newOrder = newOrder.sort((a, b) => a - b); + const middleIndex = Math.floor(newOrder.length / 2); + if (newOrder.length % 2 !== 0) { + return newOrder[middleIndex]; + } else { + const middleTwo = (newOrder[middleIndex] + newOrder[middleIndex - 1]) / 2; + return middleTwo; + } } - module.exports = calculateMedian; +console.log(calculateMedian([110, 20, 0])); +console.log(calculateMedian([3, "apple", 1, 2, undefined, 4])); +console.log(calculateMedian([])); +console.log(calculateMedian(["frog", "dog", "log", "cog"])); diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..043f3263d 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,6 @@ -function dedupe() {} +function dedupe(elements) { + const noDupe = [...new Set(elements)]; + return noDupe; +} +console.log(dedupe([3, 3, 3, 4, 5, 4, "cat", "dog", "cat", 6, 1, 2, 3, 1])); +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..a6fd5efdc 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,23 @@ 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", () => { + 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]); + expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]); +}); // 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 strings or numbers, it removes duplicates preserving first occurrence", () => { + expect(dedupe(["a", "a", "b", "c", "c"])).toEqual(["a", "b", "c"]); + expect(dedupe([1, 2, 2, 3, 4, 4])).toEqual([1, 2, 3, 4]); + expect(dedupe([4, 6, "hi", 4, 6, "hi"])).toEqual([4, 6, "hi"]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..92f3eb0bd 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,10 @@ function findMax(elements) { + const numbers = elements.filter((element) => typeof element === "number"); + if (numbers.length === 0) { + return -Infinity; + } + numbers.sort((a, b) => a - b); + return numbers[numbers.length - 1]; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..0b44a2c11 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -15,29 +15,57 @@ 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"); +test("Given an empty array Then it should return -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([2])).toEqual(2); + expect(findMax([7])).toEqual(7); + expect(findMax([-7])).toEqual(-7); + expect(findMax([70000000])).toEqual(70000000); +}); // 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 Then it should return the largest number overall", () => { + expect(findMax([2, -9])).toEqual(2); + expect(findMax([2, -4, -2, 9])).toEqual(9); +}); // 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 Then it should return the closest one to zero", () => { + expect(findMax([-2, -4, -1, -99])).toEqual(-1); + expect(findMax([2])).toEqual(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 negative numbers Then it should return the closest one to zero", () => { + expect(findMax([3.7, 3.8, 3.9])).toEqual(3.9); + expect(findMax([2.8, 2.6, 2.5, 2.1])).toEqual(2.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 just negative numbers Then it should return the closest one to zero", () => { + expect(findMax([-2, -4, -1, "p"])).toEqual(-1); + expect(findMax([2, 5, "i", 3, "r"])).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 +// Then it should return the least surprising value given how it behaves for all other input +test("Given an array with only non-number values Then it should return the least surprising value given how it behaves for all other input", () => { + expect(findMax(["cat"])).toEqual(-Infinity); + expect(findMax(["cat", "dog"])).toEqual(-Infinity); + expect(findMax(["cat", "dog", "catdog"])).toEqual(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..bf5242fd5 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,11 @@ function sum(elements) { -} + let total = 0; + const numbers = elements.filter((element) => typeof element === "number"); + numbers.forEach((numb) => { + total = total + numb; + }); + return total; +} +console.log(sum([5])); module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..6a3a159c6 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,45 @@ 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 Then it should return 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 one element it should return that number", () => { + expect(sum([4])).toEqual(4); + expect(sum([10000])).toEqual(10000); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("Given negitave numbers still sums correctly", () => { + expect(sum([2, 3, -3])).toEqual(2); + expect(sum([5, -3, -3])).toEqual(-1); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("Given a float number it still sums correctly", () => { + expect(sum([4.2, 4.3])).toEqual(8.5); + expect(sum([2.223, 5.345, 3.141])).toEqual(10.709); + expect(sum([3.887, 2.1945, -3.5])).toEqual(2.5815); +}); // 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 non-numerical values should ignore and sum the numbers", () => { + expect(sum([4, "four", "fore", "iv", 4])).toEqual(8); +}); // 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 only non-numerical values should return 0", () => { + expect(sum(["tommorow", "and", "tommorow", "and", "tommorow"])); +}); 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; }