diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..6226fbae0 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,10 +5,51 @@ // 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; +function calculateMedian(list) { + // [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ] + if (typeof list === 'string') { + return null + } + if (typeof list === 'number') { + return null + } + + if (list === null) { + return null + } + if (typeof list === 'undefined') { + return null + } + + if (list === undefined) { + return null + } + if (typeof list === 'object' && !Array.isArray(list)) { + return null + } + if (Array.isArray(list) && list.length === 0){ + return null + } + list = list.filter(element => typeof element === 'number'); + if (list.length === 0) { + return null + } + list.sort((a, b) => a - b); + + if (list.length % 2 === 0){ + const middleIndexR = Math.floor(list.length / 2); + const middleIndexL = middleIndexR - 1 + const evenMedian = (list[middleIndexL] + list[middleIndexR]) / 2; + + return evenMedian + } else { + const middleIndex = Math.floor(list.length / 2); + const median = list[middleIndex]; + return median; + } + } +calculateMedian(['apple']) + module.exports = calculateMedian; diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..edf57aef9 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -27,7 +27,7 @@ describe("calculateMedian", () => { it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) ); - it("doesn't modify the input array [3, 1, 2]", () => { + it("doesn't modify the input array [3, 1, 2]", () => { const list = [3, 1, 2]; calculateMedian(list); expect(list).toEqual([3, 1, 2]); @@ -35,7 +35,7 @@ describe("calculateMedian", () => { [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) - ); + ); [ { input: [1, 2, "3", null, undefined, 4], expected: 2 }, @@ -47,4 +47,4 @@ describe("calculateMedian", () => { ].forEach(({ input, expected }) => it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) ); -}); + }); diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..9853f4766 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(list) { + return [...new Set(list)]; +} + +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 d7c8e3d8e..853e7c12d 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 an a copy", () => { + 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 with duplicate strings and numbers, it returns the original with duplicates removed", () => { + expect(dedupe([1, 'apple', 'horse', 'horse', 2, 2, 3, 'bat'])).toEqual([1, 'apple', 'horse', 2, 3, 'bat']); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..e1b256bce 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,8 @@ function findMax(elements) { + elements = elements.filter(element => typeof element === 'number'); + if (elements.length === 0){ + return -Infinity; + } + return Math.max(...elements); } - module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..439e14d14 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,49 @@ 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 the same number", () => { + expect(findMax([4])).toEqual(4); +}); // 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([10, -10])).toEqual(10); +}); // 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 negative numbers, returns the number closest to zero", () => { + expect(findMax([-3 , -15, -4, -11])).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 negative numbers, returns the number closest to zero", () => { + expect(findMax([-3 , -15, -4, -11])).toEqual(-3); +}); // 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, ignore the non numeric values and return the max", () => { + expect(findMax(['car', 'house', 1, 12, 'fish'])).toEqual(12); +}); // 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(['car', 'house', 'fish', 'car'])).toEqual(-Infinity); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..b5d5f44d9 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,17 @@ function sum(elements) { + elements = elements.filter(element => typeof element === 'number'); + if (elements.length === 0) { + return null;} + + let total = 0; + + for (let element of elements) { + total += element; + // += means to add to the current value and assign as result + // elements are individual items inside a collection (not just string) + } + + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..e7c00ff6e 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,42 @@ 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 one number, returns the same number", () => { + expect(sum([7])).toEqual(7); +}); // 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 total sum", () => { + expect(sum([-4, -15, -12])).toEqual(-31); +}); // 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 sum", () => { + expect(sum([4.1, 5.4, 7.8])).toEqual(17.3); +}); // 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 the non-number values and returns the correct sum", () => { + expect(sum(['fish', 4, 'dog', 5, ,'pizza', 10])).toEqual(19); +}); // 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 of only non-numeric values, returns 0", () => { + expect(sum(['fish','dog','pizza'])).toEqual(0); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..6f2e347ad 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; } @@ -10,4 +9,5 @@ function includes(list, target) { return false; } + module.exports = includes;