diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..db8fbf044 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,18 @@ // 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 number = list.filter(n => typeof n === "number"); + if(number.length === 0) {return null;} + number.sort((a,b) => a-b) + const middleIndex = Math.floor(number.length / 2); + + + if(number.length%2===0){return (number[middleIndex-1] + number[middleIndex])/2; + } + else return number[middleIndex]; } -module.exports = calculateMedian; + + +module.exports = calculateMedian \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..7abb5fb70 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(list) { + return [...new Set(list)]; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..7f383480e 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,28 @@ 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", () => { + const array = []; + const currentOutput = dedupe(array); + const targetOutput = []; + expect(currentOutput).toEqual(targetOutput); +}); // 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 duplication, it copy of the original array ", () => { + const array = [1, 2, 3]; + const currentOutput = dedupe(array); + const targetOutput = [1, 2, 3]; + expect(currentOutput).toEqual(targetOutput); +}); // 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 an array with string , it removes duplication ", () => { + const array = ["a", "a", 1, 1, "ba", 4]; + const currentOutput = dedupe(array); + const targetOutput = ["a", 1, "ba", 4]; + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..6deb685c1 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,11 @@ function findMax(elements) { + let maximumNumber = -Infinity; + for (const item of elements) { + if (typeof item === "number" && item > maximumNumber) { + maximumNumber = item; + } + } + return maximumNumber; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..051215ad3 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,65 @@ 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", () => { + const maxElement = []; + const currentOutput = findMax(maxElement); + const targetOutput = -Infinity; + expect(currentOutput).toEqual(targetOutput); +}); // 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", () => { + const maxElement = [1]; + const currentOutput = findMax(maxElement); + const targetOutput = 1; + expect(currentOutput).toEqual(targetOutput); +}); // 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 one both positive and negative numbers , return largest number", () => { + const maxElement = [2, -4, 8, -1]; + const currentOutput = findMax(maxElement); + const targetOutput = 8; + expect(currentOutput).toEqual(targetOutput); +}); // 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 the closest to zero", () => { + const maxElement = [-1, -4, -12]; + const currentOutput = findMax(maxElement); + const targetOutput = -1; + expect(currentOutput).toEqual(targetOutput); +}); // 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", () => { + const maxElement = [2.5, 4.5, 9.8]; + const currentOutput = findMax(maxElement); + const targetOutput = 9.8; + expect(currentOutput).toEqual(targetOutput); +}); // 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 one non-number return the max and ignore non-numeric value, ", () => { + const maxElement = ["hello", 4, 8, 10]; + const currentOutput = findMax(maxElement); + const targetOutput = 10; + expect(currentOutput).toEqual(targetOutput); +}); // 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", () => { + const maxElement = ["history", "nation"]; + const currentOutput = findMax(maxElement); + const targetOutput = -Infinity; + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..ce2af4c93 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,11 @@ function sum(elements) { + let total = 0; + for (const element of elements) { + if (typeof element === "number") { + total += element; + } + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..11e98dc6e 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,59 @@ 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", () => { + const list = []; + const currentOutput = sum(list); + const targetOutput = 0; + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +test("given an array with one number, returns that number", () => { + const list = [20]; + const currentOutput = sum(list); + const targetOutput = 20; + expect(currentOutput).toEqual(targetOutput); +}); // 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[-20, 30, -50] , returns the correct total sum ", () => { + const list = [-20, 30, -50]; + const currentOutput = sum(list); + const targetOutput = -40; + expect(currentOutput).toEqual(targetOutput); +}); // 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 [1.5, -2.4, 4.5], returns the correct total sum", () => { + const list = [1.5, -2.4, 4.5]; + const currentOutput = sum(list); + const targetOutput = 3.6; + expect(currentOutput).toEqual(targetOutput); +}); // 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-numerical value, ignore none-numerical value, returns the sum of numerical elements", () => { + const list = [-20, "hello", 30, "a"]; + const currentOutput = sum(list); + const targetOutput = 10; + expect(currentOutput).toEqual(targetOutput); +}); // 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 based on the behavior of other inputs", () => { + const list = ["a", "b", null, undefined]; + const currentOutput = sum(list); + const targetOutput = 0; + expect(currentOutput).toEqual(targetOutput); +}); 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; }