diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..c593f1811 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,41 @@ // 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; + let newArray = []; + if(!Array.isArray(list)) + { + return null; + } + + for (const item of list) + { + if (typeof item === 'number') + { + newArray.push(item); + } + + } + if (newArray.length === 0) + { + return null; + } + newArray = newArray.sort((a, b) => a-b); + if(newArray.length %2 === 0) + { + const rightIndex = newArray.length/2 + const leftIndex = rightIndex - 1; + const median = (newArray[rightIndex] + newArray[leftIndex])/2; + return median; + + } + else{ + + const middleIndex = Math.floor(newArray.length/2); + return newArray[middleIndex] + } + //const middleIndex = Math.floor(newArray.length / 2); + //const median = list.splice(middleIndex, 1)[0]; + //return newArray[middleIndex]; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..e15439a83 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,17 @@ -function dedupe() {} +function dedupe(list) { + if (list.length=== 0) + { + return []; + } + let result = []; + for (let item of list) + { + if(!result.includes(item)) + { + result.push(item); + } + } + return result; + +} +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 23e0f8638..04d0edaa6 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -17,7 +17,18 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] // 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", ()=>{ + const list = []; + expect(dedupe(list)).toEqual([]); +}); +test +("array with no duplicate should return copy of original", ()=>{ + let list = [1,2,4,6,9,8]; + expect(dedupe(list)).toEqual([1,2,4,6,9,8]); +}); +test("shoiuld remove the duplicate value and preserving the first occurebce",()=>{ + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5,1,2,3,8]); +}) // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..e69260ad5 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,19 @@ function findMax(elements) { + if(elements.length === 0) + { + return -Infinity; + } + let max = -Infinity ; + for (const item of elements) +{ + if (typeof item === 'number') + { + max = Math.max(max , item); + } + + +} + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..afcf84bdc 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,7 +16,34 @@ 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 should return -Infinity", () => { + const list = []; + expect(findMax(list)).toEqual(-Infinity); +}); +test("Array with one number should return that number", () => { + const list =[7]; + expect(findMax(list)).toEqual(7); +}); +test("An array with both positive and negative number should return the largest overall", () =>{ + const list = [-10, 5, 30, -3]; + expect(findMax(list)).toEqual(30); +}); +test("Array with just negative number should return the closest on to zero", () =>{ + const list = [-10, -3, -20,-7,-4] + expect(findMax(list)).toEqual(-3); +}); +test("An array with decimal number should return largest decimal number", ()=>{ + const list = [1.2, 3.5, 2.8] + expect(findMax(list)).toEqual(3.5); +}); +test("An array with non-numeric values should return the max and ignore non-numeric", () =>{ + const list = ['hey', 10, 'hi', 60, 50]; + expect(findMax(list)).toEqual(60); +}); +test("An array with non numeric value should return the least surprising value given how it behaves for all other inputs", ()=> { + const list = ['a', 'b', 'hello']; + expect(findMax(list)).toEqual(-Infinity); +}) // Given an array with one number // When passed to the max function diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..7bb139801 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,19 @@ function sum(elements) { + if (elements.length === 0) + { + return 0; + } + let total = 0 ; + for (const item of elements) +{ + if (typeof item === 'number') + { + total = total + item; + } + + +} + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..0598d34ab 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,7 +13,34 @@ 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 should return -Infinity", () => { + const list = []; + expect(sum(list)).toEqual(0); +}); +test("Array with one number should return that number", () => { + const list =[7]; + expect(sum(list)).toEqual(7); +}); +test("An array with both positive and negative number should return the largest overall", () =>{ + const list = [-10, 5, 30, -3]; + expect(sum(list)).toEqual(22); +}); +test("Array with just negative number should return the closest on to zero", () =>{ + const list = [-10, -3, -20,-7,-4] + expect(sum(list)).toEqual(-44); +}); +test("An array with decimal number should return largest decimal number", ()=>{ + const list = [1.2, 3.5, 2.8] + expect(sum(list)).toEqual(7.5); +}); +test("An array with non-numeric values should return the max and ignore non-numeric", () =>{ + const list = ['hey', 10, 'hi', 60, 50]; + expect(sum(list)).toEqual(120); +}); +test("An array with non numeric value should return the least surprising value given how it behaves for all other inputs", ()=> { + const list = ['a', 'b', 'hello']; + expect(sum(list)).toEqual(0); +}) // Given an array with just one number // When passed to the sum function 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; }