diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..6b4ff09b9 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,42 @@ // 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 numbers = []; + for (let i = 0; i < list.length; i++) { + if (typeof list[i] === 'number') { + numbers.push(list[i]); + } + } + + if (numbers.length === 0) { + return null; + } + + for (let i = 0; i < numbers.length; i++) { + for (let j = i + 1; j < numbers.length; j++) { + if (numbers[i] > numbers[j]) { + const temp = numbers[i]; + numbers[i] = numbers[j]; + numbers[j] = temp; + } + } + } + + const middleIndex = Math.floor(numbers.length / 2); + + + if (numbers.length % 2 === 0) { + + return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; + } else { + + return numbers[middleIndex]; + } } + module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..a0055e476 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,15 @@ -function dedupe() {} +function dedupe(elements) { + const unique = []; + + for (let i = 0; i < elements.length; i++) { + const current = elements[i]; + + if (!unique.includes(current)) { + unique.push(current); + } + } + + return unique; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..2effce8a2 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,39 @@ 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 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("Given an array with no duplicates, it returns an the same array", () => { + const input = [1,4,6,"d","a","x","e",0,7,8]; + const currentOutput = dedupe(input); + const targetOutput = [1,4,6,"d","a","x","e",0,7,8]; + + expect(currentOutput).toEqual(targetOutput); +}); + +test("The copy must not be the very same array in memory", () => { + const input = [3, 1, 2]; + const currentOutput = dedupe(input); + + expect(currentOutput).toEqual([3, 1, 2]); + expect(currentOutput).not.toBe(input); // make sure the function returns a copy +}); // 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 +// Then it should remove the duplicate values, preserving the first occurrence of each element + +test("Given an array with strings or numbers, it returns an the array whit no duplicated elements", () => { + const currentOutput = dedupe(['a','a','a','b','b','c']); + const targetOutput = ['a','b','c']; + + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..18c819cd2 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,22 @@ function findMax(elements) { + if (elements.length === 0) { + return -Infinity; + } + + let max = -Infinity; + + for (let i = 0; i < elements.length; i++) { + let current = elements[i]; + + // using Number.isFinite keeps NaN or Infinity out of the comparison + if (typeof current === "number" && Number.isFinite(current)) { + if (current > max) { + max = current; + } + } + } + + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..9ed7f3365 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,75 @@ 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("given an array with one number, returns that number", () => { + const currentOutput = findMax([42]); + const targetOutput = 42; + + 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 positive and negative numbers, returns the largest", () => { + const currentOutput = findMax([-25, 5, 20, -3, 15]); + const targetOutput = 20; + + 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 of only negative numbers, returns the closest to zero", () => { + const currentOutput = findMax([-50, -3, -20, -10]); + const targetOutput = -3; + + 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 of decimal numbers, returns the largest decimal", () => { + const currentOutput = findMax([1.1, 3.5, 2.9, 3.4]); + const targetOutput = 3.5; + + 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 non-number values, returns correct max (ignoring non-numbers)", () => { + const currentOutput = findMax([10, "hi", 50, true, 3]); + const targetOutput = 50; + + 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 currentOutput = findMax(["a", null, undefined, {}, [], true]); + const targetOutput = -Infinity; + + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..317b386f9 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,20 @@ function sum(elements) { + // This variable will store the total sum + let total = 0; + + // Go through each element in the array + for (let i = 0; i < elements.length; i++) { + let current = elements[i]; // get the current element + + // Only add it if it's a number + // Number.isFinite skips NaN and Infinity, so only real numbers add to total + if (typeof current === "number" && Number.isFinite(current)) { + total = total + current; // add to total + } + } + + // Return the sum of all numbers + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..f6ae98d1f 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,64 @@ 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 targetPitPut= 0; + + expect(currentOutput).toEqual(targetPitPut); +}) + // Given an array with just one number // When passed to the sum function // Then it should return that number +test("given an array whit only 1 number, returns same input",()=>{ + const currentOutput = sum([4]); + const targetPitPut= 4; + + expect(currentOutput).toEqual(targetPitPut); +}) + // 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 whit a negative number, returns correct total (subtract the negative number)",()=>{ + const currentOutput = sum([1,2,3,4,-5,]); + const targetPitPut= 5; + + expect(currentOutput).toEqual(targetPitPut); +}) + // 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 whit a decimal/float number, returns correct total (decimal/float number)",()=>{ + const currentOutput = sum([1,2,3,4,3.5,]); + const targetPitPut= 13.5; + + expect(currentOutput).toBeCloseTo(targetPitPut); // allow tiny floating point differences +}) + + // 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 whit a non-number values, returns correct total (ignore the NaN and sum the others)",()=>{ + const currentOutput = sum([1,2,3,4,"hi",5]); + const targetPitPut= 15; + + expect(currentOutput).toEqual(targetPitPut); +}) // 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 (0)", () => { + const currentOutput = sum(["hello", null, undefined, {}, [], true, NaN]); + const targetOutput = 0; + + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..3001fa212 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +1,12 @@ // 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; + return true; } } - return false; + return false; } module.exports = includes;