diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..1cf63a948 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,10 +5,24 @@ // 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){ + if(!Array.isArray(list)){ + return null; + } + const num = []; + for(let i = 0; i < list.length ; i++){ + if (typeof list[i] === "number"){ + num.push(list[i]); + } + } + if (num.length === 0){ + return null; + } + num.sort((a, b) => a - b); + const middleIndex = Math.floor(num.length / 2); + if (num.length % 2 === 1){ + return num[middleIndex]; + } + return (num[middleIndex-1] + num[middleIndex])/2; } - module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..110833015 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,10 @@ -function dedupe() {} +function dedupe(array) { + const result = []; + for(let i = 0; i < array.length ; i++){ + if(!result.includes(array[i])){ + result.push(array[i]); + } + } + return result; +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..b1362c019 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,20 @@ 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.todo("given an empty array, it returns an empty array"); +test("given an empty array, returns 0", () => { + 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 duplicate, returns original array", () => { + expect(dedupe([2,7,9])).toEqual([2,7,9]); +}); // 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 string or numbers, returns with no duplicates", () => { + expect(dedupe([4, "banana", 4, "banana", 2])).toEqual([4, "banana", 2]); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..e5c0e9232 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,16 @@ function findMax(elements) { + if (elements.length === 0){ + return -Infinity; + } + let max = -Infinity; + for(let item of elements){ + if(typeof item !== "number"){ + continue; + } + if(item > 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..13d2bb9a4 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,43 @@ 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 empty array, returns 0", () => { + expect(findMax([78])).toBe(78); +}); // 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 number", () => { + expect(findMax([-8, 10, 45, -60])).toBe(45); +}); // 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 0", () => { + expect(findMax([-8, -7, -5, -60])).toBe(-5); +}); // 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 largest number", () => { + expect(findMax([3.7, 5.8, 4.5, 60.8])).toBe(60.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 number", () => { + expect(findMax([100, "hello", 94])).toBe(100); +}); // 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 non-numbers, returns -Infinity", () => { + expect(findMax(["hi", "maryam", "here"])).toBe(-Infinity); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..e9e002f91 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,14 @@ function sum(elements) { -} + let result = 0; + if(elements.length === 0){ + return 0; + } + for(let i = 0; i < elements.length; i++){ + if(typeof(elements[i]) == "number"){ + result = result + elements[i]; + } + } + return result; +} module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..79afc72a6 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.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 1 value, returns that value", () => { + expect(sum([50])).toBe(50); +}); // 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 correct sum", () => { + expect(sum([9, 2, 3, -4])).toBe(10); +}); // 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 numbers, returns correct sum", () => { + expect(sum([4.5, 9.2, 9, 3.2])).toBe(25.9); +}); // 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 only values", () => { + expect(sum([5, "banana", 2])).toBe(7); +}); // 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 0", () => { + expect(sum(["hi", "here"])).toBe(0); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..d50d3650d 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +1,10 @@ // 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) { + for(const element of list){ + if(element === target ){ return true; } } return false; } - -module.exports = includes; +module.exports = includes; \ No newline at end of file