diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..683b74768 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,36 @@ // 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; + //checking if the provided list is an Array + if (!Array.isArray(list)) { + return null; + } + + //need to filter the array to check only for numbers + let filteringArr = list.filter((item) => { + return typeof item === "number"; + }); + + //handel no numbers + if (filteringArr.length === 0) { + return null; + } + // sorting the array + filteringArr.sort((a, b) => a - b); + + // calculating middle index + const middleIndex = Math.floor(filteringArr.length / 2); + + // finding Odd length + if (filteringArr.length % 2 === 1) { + const median = filteringArr[middleIndex]; + + return median; + } else { + return (filteringArr[middleIndex - 1] + filteringArr[middleIndex]) / 2; + } + + // return median; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..11f4f64c4 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(arr) { + let newArray = []; + + for (let i = 0; i < arr.length; i++) { + if (!newArray.includes(arr[i])) { + newArray.push(arr[i]); + } + } + return newArray +} + +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..cb675ac4b 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,44 @@ 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("if receive an empty array should return an empty ", () => { + const currentOutput = dedupe([]); + 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("the functions should remove the duplicated characters and return it inside a new array ", () => { + const currentOutput = dedupe([1, 2]); + const targetOutput = [1, 2]; + + expect(currentOutput).toEqual(targetOutput); +}); // 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 +// Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. +test("the functions should remove the duplicated characters and return it inside a new array ", () => { + const currentOutput = dedupe(['a','a','a','b','b','c']); + const targetOutput = ['a','b','c']; + + expect(currentOutput).toEqual(targetOutput); +}); + +test("the functions should remove the duplicated characters and return it inside a new array ", () => { + const currentOutput = dedupe([5, 1, 1, 2, 3, 2, 5, 8]); + const targetOutput = [5, 1, 2, 3, 8]; + + expect(currentOutput).toEqual(targetOutput); +}); + +test("the functions should remove the duplicated characters and return it inside a new array ", () => { + const currentOutput = dedupe([1, 2, 1]); + const targetOutput = [1, 2]; + + expect(currentOutput).toEqual(targetOutput); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..c011d40c1 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,19 @@ function findMax(elements) { + let largestNum = -Infinity + + let filteringArr = elements.filter((item) => { + return typeof item === "number"; + }); + + + for (let i = 0; i < filteringArr.length; i++) { + if (filteringArr[i] > largestNum) { + + largestNum = filteringArr[i]; + } + } + return largestNum; } module.exports = findMax; + diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..51da4567e 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,74 @@ 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 currentOutput = (findMax([])); + 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("should return the largest number", () => { + const currentOutput = (findMax([30])); + const targetOutput = 30; + + 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("should return the largest number", () => { + const currentOutput = (findMax([30, 50, -10, 40])); + const targetOutput = 50; + + 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("should also return the largest negative number", () => { + const currentOutput = (findMax([-30, -50, -10, -40])); + const targetOutput = -10; + + 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("should return the largest number even if there are decimal numbers", () => { + const currentOutput = (findMax([30.4, 30.9, 10, -40])); + const targetOutput = 30.9; + + 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("should skip the string and return the largest number", () => { + const currentOutput = (findMax([30, "aa", 10, 40])); + const targetOutput = 40; + + 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("should skip all the string and return should be -Infinity", () => { + const currentOutput = (findMax(["bb", "aa", "cc"])); + const targetOutput = -Infinity; + + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..2d747e790 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,13 @@ function sum(elements) { -} + let sum = 0; + + let filteringArr = elements.filter((item) => { + return typeof item === "number"; + }); + for (let i = 0; i < filteringArr.length; i++) { + sum += filteringArr[i]; + } + return sum; +} module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..cf4917d65 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,58 @@ 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 currentOutput = sum([]); + 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 only one number the test should return that number", () => { + const currentOutput = sum([20]); + 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 only one number the test should return that number", () => { + const currentOutput = sum([-20, -30, -2, 4]); + const targetOutput = -48; + + 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 numbers the test should return the total", () => { + const currentOutput = sum([2.0, 3.3, 1.2, 4.4]); + const targetOutput = 10.9; + + 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-numbers the test should skip the string and return the total", () => { + const currentOutput = sum([3, 3.3, "aa", "4.4"]); + const targetOutput = 6.3; + + 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-numbers the test should return the least surprising value given how it behaves for all other inputs", () => { + const currentOutput = sum(["cc", "3", "aa", "4.4"]); + const targetOutput = 0; + + expect(currentOutput).toEqual(targetOutput); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..994131c59 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 (let element of list) { if (element === target) { return true; }