diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..927259d82 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,37 @@ // 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; + + // Check valid input -array + if(!Array.isArray(list) || list.length === 0) return null; + + // Filter non-numeric values + + const numbers = list.filter(item => typeof item === 'number'); + if (numbers.length === 0) return null; + + // Sort the array + + const sorted = [...numbers].sort((a, b) => a - b); + + // Get the index of the middle item by rounding it using Math.floor() + + const mid = Math.floor(sorted.length/2); + + // for Odd number of items return the middle element + + if (sorted.length % 2 !== 0) return sorted[mid]; + + // for even number of items + return (sorted[mid - 1] + sorted[mid]) / 2; + } module.exports = calculateMedian; + + +// Implementation fixed + + // const middleIndex = Math.floor(list.length / 2); + // const median = list.splice(middleIndex, 1)[0]; + // return median; diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..b637ac950 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -47,4 +47,4 @@ 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..949208ce5 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,8 @@ -function dedupe() {} +function dedupe(array) { + if (!Array.isArray(array)) throw new Error("Input must be an array"); + return array.filter((item, index) => array.indexOf(item) === index); +} +console.log(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])); +module.exports = dedupe; + +// Dedupe function implemented. \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..87052d61a 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,32 @@ 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 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']); +}); + // 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 duplicate strings removes duplicate strings", () => { + expect(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']); +}); + +// + +test("Given an array removes duplicates but keeps first occurrence", () => { + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); + +// Dedupe function implemented, test cases.. performed. \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..7e29f6a15 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,22 @@ -function findMax(elements) { +function findMax(list) { + + // check if input is array + + if (!Array.isArray(list)) { + throw new Error("Input must be an array"); + } + + // filter out non-numeric values + list = list.filter((list) => typeof list === "number"); + + if (list.length === 0) { + return -Infinity; + } + if (list.length === 1) { + return list[0]; + } + + return Math.max(...list); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..0cf9c58ea 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,57 @@ 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([])).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 one number, returns that number", () => { + expect(findMax([42])).toEqual(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 positive and negative numbers, returns the largest", () => { + expect(findMax([-10, 20, 5, -1])).toEqual(20); +}); + // 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 only negative numbers, returns the closest to zero", () => { + expect(findMax([-50, -10, -100])).toEqual(-10); +}); + // 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.2, 3.5, 2.8])).toEqual(3.5); +}); + // 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, ignores them and returns the max number", () => { + expect(findMax(["a", 10, "b", 60, 10])).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 with only non-number values, returns -Infinity", () => { + expect(findMax(["a", "b", null, undefined])).toEqual(-Infinity); +}); + +// max.test.js test cases performed and function implemented \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..d2df411e5 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,7 @@ function sum(elements) { + return elements + .filter(elements => typeof elements === "number" && !Number.isNaN(elements)) + .reduce((total, elements) => total + elements, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..bc7ebe9bc 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,47 @@ 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([])).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, returns that number", () => { + expect(sum([5])).toEqual(5); +}); // 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 sum", () => { + expect(sum([-5, 10, -5])).toEqual(0); +}); + // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given decimal numbers, returns the correct sum", () => { + expect(sum([1.5, 2.5])).toEqual(4); +}); + // 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 non-number values, ignores them and returns sum of numeric values", () => { + expect(sum(["hey", 10, "hi", 60, 10])).toEqual(80); +}); + // 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 only non-number values, returns 0", () => { + expect(sum(["a", "b", null, undefined])).toEqual(0); +}); + +// Sum.test.js function implemented and tested. \ No newline at end of file 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; } diff --git a/Sprint-1/refactor/includes.test.js b/Sprint-1/refactor/includes.test.js index 812158470..1a56dd7a3 100644 --- a/Sprint-1/refactor/includes.test.js +++ b/Sprint-1/refactor/includes.test.js @@ -36,3 +36,6 @@ test("searches for null", () => { expect(currentOutput).toEqual(targetOutput); }); + + +// include.test.js passed the test after refactoring the include-function with (for ... of) \ No newline at end of file diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..8c1cad305 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,28 @@ +// We need to import file system and path for our task + +const fs = require("fs"); +const path = require("path"); + +function solution() { + + // set file path + + const inputFilePath = path.join(__dirname, "input.txt"); + + // read the file content + let fileContent = fs.readFileSync(inputFilePath, "utf-8"); + + // split the content into lines and convert each line to a number + let numbersArray = fileContent.split("\n").map(num => Number(num)); + + // calculate the sum of all numbers + const sumOfNumbers = numbersArray.reduce((sum, num) => sum + num, 0); + + return sumOfNumbers; +} + +console.log(solution()); + +module.exports = solution; + +// A solution to add-up frequency values in input.txt \ No newline at end of file