diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..fc7429342 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,29 @@ // 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; + // validate the array + if (!Array.isArray(list)){ + return null; + } + // keep only the real number by filtering the array( excluding string, null, undefined ) + const numbersOnly = list.filter((item) => typeof item === 'number') + // after filtering return null if nothing is valid + if (numbersOnly.length === 0){ + return null ; + } + const sorted =[...numbersOnly].sort((a,b) => a-b) + const middleIndex = Math.floor(sorted.length / 2) + + + if (sorted.length %2 == 0){ + return (sorted[middleIndex-1] + sorted[middleIndex])/2; + }else{ + return sorted[middleIndex] + } + + + + } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..71474ddab 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(arr) { + // initialize an empty array + let dedupedArr =[]; + for( element of arr){ + if(!dedupedArr.includes(element)){ + dedupedArr.push(element); + } + + } + return dedupedArr +} +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..76601e0c0 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,4 +1,5 @@ const dedupe = require("./dedupe.js"); + /* Dedupe Array @@ -16,13 +17,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, returns a copy of the original array", ()=>{ + expect(dedupe([3,4,6])).toEqual([3,4,6]) +}) // 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 of strings or numbers, returns a new array with duplicates removed while preserving the first occurrence of each element from the original array',()=>{ + expect(dedupe(['h',4,'k','h',3,5,4])).toEqual(['h',4,'k',3,5]) +} +) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..047eb8426 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,15 @@ function findMax(elements) { + const numbersOnly = elements.filter((item)=> typeof item === 'number' && !Number.isNaN(item)); + + if (numbersOnly.length ===0 ){ + return -Infinity + } + const sorted = [...numbersOnly].sort((a,b) => a-b) + const max = sorted[sorted.length-1] + + return max + + } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..ec67685ca 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,47 @@ 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([])).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([22])).toBe(22) +} ) // 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([-3,6,-9])).toBe(6) +} +) // 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, returns the closest one to zero", ()=>{ + expect(findMax([-3,-4,-1])).toBe(-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 numbers, returns the largest decimal number', ()=>{ + expect(findMax([3.5,4.5,6.5])).toBe(6.5) +}) // 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 the max and ignore non-numeric values", () =>{ + expect(findMax(["3",5,'undefined',null,6])).toBe(6) +}) // 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(['Holla!', NaN, true])).toBe(-Infinity) +}) \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..4f91bb8f7 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,15 @@ function sum(elements) { + //filter the array to be numerical values only + const numbersOnly = elements.filter((item)=> typeof item === 'number' && !Number.isNaN(item)); + + if(numbersOnly.length === 0){ + return 0 + } + let total = 0 ; + for( let i = 0 ; i < numbersOnly.length; i ++ ){ + total += numbersOnly[i]; + } + return total ; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..95707a95f 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,39 @@ 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([])).toBe(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, return that number', ()=>{ + expect(sum([3])).toBe(3) +}) // 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 with negative numbers, return the correct total', ()=>{ + expect(sum([-2,-4,-1])).toBe(-7) +}) // 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([3.5,6.1,])).toBe(9.6) +} ) // 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, return the sum of the numerical elements and ignore the non-numerical values',()=>{ + expect(sum(['4',"Holla",6,9,1])).toBe(16) +}) // 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, return -Infinity',()=>{ + expect(sum([null,undefined,NaN,true])).toBe(0) +}) \ No newline at end of file diff --git a/Sprint-1/package.json b/Sprint-1/package.json index 330755010..9901d571a 100644 --- a/Sprint-1/package.json +++ b/Sprint-1/package.json @@ -11,5 +11,6 @@ "license": "ISC", "devDependencies": { "jest": "^29.7.0" - } + }, + "type": "commonjs" } diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..5d794778f 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +1,21 @@ // 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; +// 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 (element of list){ + if(element === target){ + return true } } - return false; + return false } module.exports = includes;