diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..5883f864a 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,28 @@ // 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)) { + return null; + } + + const nums = list.filter(i => typeof i === "number" && !isNaN(i)); + + if (nums.length === 0) { + return null; + } + + const sorted = [...nums].sort((a, b) => a - b); + + const len = sorted.length; + const mid = Math.floor(len / 2); + + + if (len % 2 === 1) { + return sorted[mid]; + } + + return (sorted[mid - 1] + sorted[mid]) / 2; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..8709f15ca 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,8 @@ -function dedupe() {} +function dedupe(arr) { + return [...new Set (arr)]; +} +console.log(dedupe([1, 2, 3])); +console.log(dedupe([1, 1, 2, 3, 2, 3, 3])); +console.log(dedupe([])); +console.log(dedupe(["hello", undefined, 5, 5, "hello"])); + diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..bc6c95743 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,20 @@ 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]); +}); // 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 +// Then it should remove the duplicate values, preserving the first occurrence of each element +test("Given an array with strings or numbers, remove the duplicate values, preserving the first occurrence of each element", () => { + expect(dedupe(["hello", undefined, 5, 5, "hello"])).toEqual(["hello", undefined, 5,]); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..83def2214 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,12 @@ function findMax(elements) { + const nums = elements.filter(i => typeof i === "number" && !isNaN(i)); + + if (nums.length === 0) { + return "-infinity"; + } else { + return Math.max(...nums); + } + } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..d6e158716 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,44 @@ 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 that number", () => { + expect(findMax[50]).toEqual([50]); +}) // 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 positive and negative numbers; returns largest overall", () => { + expect(findMax[-1, -3, 5, 7, 8]).toEqual(8); +}); // 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 closest to zero", () => { + expect(findMax[-7, -5, -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 decimal numbers, returns the largest decimal number", () => { + expect(findMax[2.34, 2.35]).toEqual(2.35); +}); // 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-numbers, returns the max and ignore non-numeric values", () => { + expect(findMax["hello", 2, 5, -1]).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-numbers values, returns the largest surprising ", () => { + expect(findMax["5", "hello", "a2"]).toEqual("-infinity"); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..d4c6fd8dc 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,19 @@ -function sum(elements) { +function sum(elements) { + const numbers = elements.filter((i) => typeof i === "number" && !isNaN(i)); + + if (numbers.length === 0) { + return 0; + } + + return numbers.reduce((total, num) => total + num, 0); } +console.log(sum([10, 30, 45])); +console.log(sum([10])); +console.log(sum([-10, -30, -45])); +console.log(sum([])); +console.log(sum([10.5, 30, 45])); +console.log(sum([NaN, null, 45, 10])); +console.log(sum([NaN, null, "hello"])); + module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..055a09239 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,37 @@ 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[10, 20, 35]).toEqual(85); +}); // 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[-10, -30, -45]).toEqual(-85); +}); // 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[10.5, 30, 45]).toEqual(85.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, ignore the non-numerical values and return the sum of the numerical elements", () => { + expect(sum[NaN, null, 45, 10]).toEqual(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 the least surprising value", () => { + expect(sum[NaN, null, "hello"]).toEqual(55); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8ddbb4dcc 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +1,13 @@ // 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; } } - return false; + return false; // return false added } module.exports = includes;