diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..5d69334a4 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 numericValue = list.filter(n => typeof n === 'number' && !isNaN(n)); + if (numericValue.length === 0) return null; + + const sorted = [...numericValue].sort((a, b) => a - b); + + const middleIndex = Math.floor(sorted.length / 2); + + return sorted.length % 2 === 0 + ? (sorted[middleIndex - 1] + sorted[middleIndex]) / 2 + : sorted[middleIndex]; } -module.exports = calculateMedian; +module.exports = calculateMedian; \ No newline at end of file diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..f835df31b 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -6,7 +6,7 @@ const calculateMedian = require("./median.js"); -describe("calculateMedian", () => { + [ { input: [1, 2, 3], expected: 2 }, { input: [1, 2, 3, 4, 5], expected: 3 }, @@ -47,4 +47,3 @@ describe("calculateMedian", () => { ].forEach(({ input, expected }) => it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) ); -}); diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..52a75d491 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,6 @@ -function dedupe() {} +function dedupe(groupNumber) { + return [...new Set(groupNumber)]; + +} + +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..b449ad401 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,33 @@ 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 input = []; + const result = dedupe(input); + expect(result).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", () => { + const input = [1, 2, 3, 4, 5]; + const result = dedupe(input); + expect(result).toEqual([1, 2, 3, 4, 5]); +}); // 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 a strings", () => { + const input = ["a", "a", "b", "b", "c", "c", "d", "d", "e", "e"]; + const result = dedupe(input); + expect(result).toEqual(["a", "b", "c", "d", "e"]); +}); + +test("given an array with number", () => { + const input = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]; + const result = dedupe(input); + expect(result).toEqual([1, 2, 3, 4, 5]); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..2a7540727 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,8 @@ function findMax(elements) { + if (!Array.isArray(elements)) return null + const maxNum = elements.filter(n => typeof n === 'number' && !isNaN(n)) + return Math.max(...maxNum) } + module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..19eab5400 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,70 @@ 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, return-Infinity", () => { + const input = []; + const result = findMax(input); + expect(result).toEqual(-Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("given an array with ine number, return-that number", () => { + const input = [10]; + const result = findMax(input); + expect(result).toEqual(10); +}); + + // 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, return-the largest number", () => { + const input = [10, 70, 5, -30, 20, -10, -100]; + const result = findMax(input); + expect(result).toEqual(70); +}); + // 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 number", () => { + const input = [-10, -30, -5, -40, -50]; + const result = findMax(input); + expect(result).toEqual(-5); +}); + + // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("given an empty array, return-Infinity", () => { + const input = [0.1, 0.5, 0.03, 0.009, 0.9, 1.1, 7.9 ]; + const result = findMax(input); + expect(result).toEqual(7.9); +}); + // 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, return-Infinity", () => { + const input = [10, 5, "banana", 60, "goat", "zebra", 5] + const result = findMax(input); + expect(result).toEqual(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, return-Infinity", () => { + const input = ["car", "train", "bus", "bmw"] + const result = findMax(input); + expect(result).toEqual(-Infinity) +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..be6cce002 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,7 @@ function sum(elements) { -} + if (!Array.isArray(elements)) return null + const sumNum = elements.filter(n => typeof n === 'number' && !isNaN(n)) + return sumNum.reduce((accumulator, currentValue) => accumulator + currentValue, 0); +}; module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..057b4fe44 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,53 @@ 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, return 0", () => { + const input = []; + const result = sum(input); + expect(result).toEqual(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 one number", () => { + const input = [10]; + const result = sum(input); + expect(result).toEqual(10); +}); // 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 a negative number", () => { + const input = [-10, -20, -30, -40]; + const result = sum(input); + expect(result).toEqual(-100); +}); // 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 number", () => { + const input = [0.1, 0.2, 0.3, 0.4]; + const result = sum(input); + expect(result).toEqual(1); +}); // 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", () => { + const input = [10,"glasgow", 30, "london", 50, "asmara", 60, "massawa"]; + const result = sum(input); + expect(result).toEqual(150); +}); // 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 containing non-number", () => { + const input = ["Nairobi","Glasgow", "London", "Asmara","Edinburgh", "Massawa"]; + const result = sum(input); + expect(result).toEqual(0); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..3a4dc227f 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,12 +1,13 @@ // 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 false; } diff --git a/Sprint-1/refactor/includes.test.js b/Sprint-1/refactor/includes.test.js index 812158470..8a51f2103 100644 --- a/Sprint-1/refactor/includes.test.js +++ b/Sprint-1/refactor/includes.test.js @@ -36,3 +36,4 @@ test("searches for null", () => { expect(currentOutput).toEqual(targetOutput); }); + diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..45faff635 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,10 @@ +function calibrateFrequency(changesInTime) { + // we need to Start from a frequency of 0 + let frequency = 0; + + // Apply each change in order + for (let change of changesInTime) { + frequency += parseInt(change, 10); + } + + return frequency; \ No newline at end of file