diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..3e28e6395 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) { + // checks if "list" is an array if not return's null. + if (!Array.isArray(list)) return null; + + // filter the non number values out of an array like "strings", and checks for " Infinte, -Infinte, null's and undefined" + const numbers = list.filter(value => Number.isFinite(value)); + + // if no numbers are left return null + if (numbers.length === 0) return null; + + // sort numbers into ascending order + numbers.sort(function (a, b) { + return a - b; + }); + + // finds the middle index based on filtered numbers length const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; -} -module.exports = calculateMedian; + // return the correct median based on if its even or odd + if (numbers.length % 2 === 0) { + // even length average of two middle numbers + return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; + } else { + // odd length returns the middle number + return numbers[middleIndex]; + } +} diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..340589dfe 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,17 @@ -function dedupe() {} +function dedupe(array) { + // makes a new array to store non duplicate values + const newArray = []; + + // loops over each item in the original array + for (const item of array) { + // checks if the values are not in the "newArray" + if (!newArray.includes(item)) { + // if item is not in "newArray" adds it to the array + newArray.push(item); + } + } + // returns the new deduped array + 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 23e0f8638..1bba725c6 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", () => { + 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, it returns a copy of the original array", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]); + + const original = [1, 2, 3]; + const result = dedupe(original); + + expect(result).toEqual(original); + expect(result).not.toBe(original); +}); // 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 duplicates it removes any duplicates and preserves the first instance of each element", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..77595f102 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,31 @@ function findMax(elements) { -} + // makes an array to hold only numbers + const numbersOnly = []; + + // to loop through each element in the array + for (const item of elements) { + // if the item is a number push it to the numbersOnly array + if (typeof item === `number`) { + numbersOnly.push(item); + } + } + // if the no numbers are found in the array returns -infinty + if (numbersOnly.length === 0) { + return -Infinity; + } + // start with the first number in the "numbersOnly" array + let max = numbersOnly[0]; + + // loop for the largest number in the "numbersOnly" array + for (const num of numbersOnly) { + if (num > max) { + max = num; + } + } + return max; +} +console.log(findMax([30, 50, 10, 40])); // 50 +console.log(findMax(["hey", 10, "hi", 60, 10])); // 60 +console.log(findMax([])); // -infinity module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..81f185eda 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 one number", () => { + expect(findMax([42])).toBe(42); +}); // 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", () => { + expect(findMax([-10, 5, -30, 27])).toBe(27); +}) // 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([-100, -10, -5, -50])).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 the largest decimal", () => { + expect(findMax([1.5, 2.3, 0.7, ,3.14, 2.71])).toBe(3.14); +}); // 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 removes the non-numeric values", () => { + expect(findMax([`hey`, 10, `hi`, 60, 10])).toBe(60); +}) // 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([`hey`, `hi`, `hello`])).toBe(-Infinity); +}) \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..46da635f2 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,17 @@ function sum(elements) { + if (elements.length === 0) return 0; // if array is input is empty returns 0 + + let total = 0; // create a variable to hold the sum + + // iterate over each element in the array + for (const value of elements){ + // checks if each of the values are numbers + if (typeof value === `number`){ + // adds the numbers to the total sum + total += value; + } + } +return total; // returns the total sum after the loop has finished adding all the numbers } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..b60ee18c1 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,42 @@ 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 a single number return that number", () => { + expect(sum([42])).toBe(42); +}) // 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 returns the correct total sum", () => { + expect(sum([-10, -20, -30])).toBe(-60); + expect(sum([-10, 20, -30])).toBe(-20); +}); // 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 in returns the correct total sum", () => { + expect(sum([10.5, 20.2, 30.3])).toBe(61.0); + expect(sum([10.5, -20.2, 30.3])).toBe(20.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-numerical values ignore non-numerical and returns correct total sum", () => { + expect(sum([2, "hello", 26.5, "hey", -6.6, 8])).toBeCloseTo(29.9); +}); // 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-numerical values returns 0", () => { + expect(sum(["a", "b", "c"])).toBe(0); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..5beeaf0e2 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,12 +1,15 @@ // 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]; +// iterate through each element in the list + for (const element of list){ + + // If an element matches the target, return true if (element === target) { return true; } } + // If no element matches the target, return false return false; }