diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index f74d1dc0a..092a66bf3 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -3,9 +3,19 @@ // If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory function calculateMedian(list) { + // const oddMiddleIndex = Math.floor(list.length / 2); + // const oddMedian = list.splice(oddMiddleIndex, 1)[0]; const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; -} + // const evenMiddleIndex = list.length / 2; + // const evenMedian = ((list.splice(evenMiddleIndex -1 ,1)[0]) + (list.splice(evenMiddleIndex ,1)[0])) / 2; + if (list.length % 2 === 1) return middleIndex ; + else return (list[middleIndex - 1] + list[middleIndex]) / 2; +}; + module.exports = calculateMedian; + +console.log(calculateMedian([1, 2, 3, 4, 5])); +console.log(calculateMedian([1, 2, 3, 4, 5, 6])); +console.log(calculateMedian([1, 2, 3, 4])); +console.log(calculateMedian([1, 2, 3])); \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..dccdf120c 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,22 @@ -function dedupe() {} +function dedupe(elements) { + if (elements.length === 0) return []; + else if (elements.length === 1) return elements; + + for (let i = elements.length -1; i >= 0; i--) { + for (let j = 0; j < i; j++) { + if (elements[j] === elements[i]) { + elements.splice(i, 1); + break; + } + } + } + return elements; +}; + +module.exports = dedupe; + +console.log(dedupe([])); +console.log(dedupe(["a", "b", "c"])); +console.log(dedupe(['a','a','a','b','b','c'])); +console.log(dedupe([5, 1, 1, 2, 3, 2, 5, 8])); +console.log(dedupe([1, 2, 1])); diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..0f034290e 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,22 @@ 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 a no duplicated array, return original array", () => { + 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 a duplicated array", () => { + 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]); + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..1988b9457 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,20 @@ -function findMax(elements) { -} +function findMax(elements) { + let numberOnly = elements.filter(item => typeof item === "number"); + // let nonNumber = elements.filter(item => typeof item !== "number"); + if (elements.length === 0) return -Infinity; + else if (elements.length === 1 && elements[0] > 0) return elements[0]; + // else if (typeof elements !== "number") return NaN + else if (elements.every(item => typeof item !== "number")) return NaN; + else return Math.max(...numberOnly); + }; + module.exports = findMax; + +console.log(findMax([])); +console.log(findMax([10])); +console.log(findMax([1, -5, 9, -10])); +console.log(findMax([-1, -3, -2])); +console.log(findMax([0.7, 1.2, 9.8, -11.3])); +console.log(findMax(["Apple", true, 10, 98])); +console.log(findMax([true, "Apple", false, "Banana"])); \ No newline at end of file diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..4f409518d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,50 @@ 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([10])).toEqual(10); +}); // 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 number, returns largest number", () => { + expect(findMax([1, -5, 9, -10])).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 number, returns the closest one to zero", () => { + expect(findMax([-1, -3, -2])).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 number, returns largest decimal number", () => { + expect(findMax([0.7, 1.2, 9.8, -11.3])).toEqual(9.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 non-number values, returns largest number", () => { + expect(findMax(["Apple", true, 10, 98])).toEqual(98); +}); + // 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 largest number", () => { + expect(findMax([true, "Apple", false, "Banana"])).toEqual(NaN); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..ea7fa5863 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,24 @@ function sum(elements) { -} + if (elements.length === 0) return 0; + else if (elements.every(item => typeof item !== "number")) return NaN; + else if (elements.length === 1) return elements[0]; + + let sumOfElements = 0; + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number") { + sumOfElements += elements[i]; + } + } + return sumOfElements; +}; module.exports = sum; + +console.log(sum([])); +console.log(sum([1])); +console.log(sum([-1, 1, -2, 3])); +console.log(sum([1.1, 2.2, 3, 4.5])); +console.log(sum(['hey', 10, 'hi', 60, 10])); +console.log(sum(['hey', true, 'hi'])); + + diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..ddf50d1a2 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,41 @@ 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 just one number array, returns that number", () => { + expect(sum([1])).toEqual(1); +}) // 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 number, returns correct total sum", () => { + expect(sum([-1, 1, -2, 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 an array with decimal/float number, returns correct total sum", () => { + expect(sum([1.1, 2.2, 3, 4.5])).toEqual(10.8); +}); // 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, returns sum of numerical elements and ignore non-numerical values", () => { + expect(sum(['hey', 10, 'hi', 60, 10])).toEqual(80); +}); // 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", () => { + expect(sum(['hey', true, 'hi'])).toEqual(NaN); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8c69ba617 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,9 +1,18 @@ // 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) { +// return true; +// } +// } +// return false; +// } + function includes(list, target) { - for (let index = 0; index < list.length; index++) { - const element = list[index]; - if (element === target) { + for (const items of list) { + if (items === target) { return true; } } @@ -11,3 +20,9 @@ function includes(list, target) { } module.exports = includes; + +console.log(includes(["a", "b", "c", "d"], "c")); +console.log(includes([1, 2, 3, 4], "a")); +console.log(includes([1, 2, 2, 3], 2)); +console.log(includes([])); +console.log(includes(["b", "z", null, "a"], null)); \ No newline at end of file