diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..cd30fcc9a 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) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + // Check if input is an array + if (!Array.isArray(list)) { + return null; + } else { + // Get only numeric values + const numericList = list.filter((item) => typeof item === "number"); + // Check if its empty after filtering + if (numericList.length === 0) { + return null; + } + // Sort the list by numeric value + numericList.sort((a, b) => a - b); + // Get middle index + const middleIndex = Math.floor(numericList.length / 2); + // Check if length is even or odd + if (numericList.length % 2 === 0) { + // Return average of two middle numbers + return (numericList[middleIndex - 1] + numericList[middleIndex]) / 2; + } else { + // Return middle number by index + return numericList[middleIndex]; + } + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..8b1fa04fd 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,23 @@ -function dedupe() {} +function dedupe(input) { + // Check if the input is an array + if (!Array.isArray(input)) { + return null; + } + // Check for empty array + if (input.length === 0) { + return []; + } + // New array to hold deduped values + const newArray = []; + // Go through each element in the input array + for (let i = 0; i < input.length; i++) { + const element = input[i]; + // If the element is not already in newArray, add it + if (!newArray.includes(element)) { + newArray.push(element); + } + } + return newArray; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..1b5df310f 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,7 +16,6 @@ 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"); // Given an array with no duplicates // When passed to the dedupe function @@ -25,3 +24,23 @@ test.todo("given an empty array, it returns an empty array"); // 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 + +describe("dedupe", () => { + [ + { input: [], expected: [] }, + { input: ["apple", "banana", 1, 10], expected: ["apple", "banana", 1, 10] }, + { + input: [1, -1, -100, -100, "apple", "apple"], + expected: [1, -1, -100, "apple"], + }, + { input: [-10, -20, -3, -4], expected: [-10, -20, -3, -4] }, + { input: [1.5, 10.5, 0.5], expected: [1.5, 10.5, 0.5] }, + { + input: ["apple", "banana", "cherry"], + expected: ["apple", "banana", "cherry"], + }, + ].forEach(({ input, expected }) => + it(`returns the deduped array for [${input}]`, () => + expect(dedupe(input)).toEqual(expected)) + ); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..fca1943df 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,16 @@ function findMax(elements) { + // Check if input is an array + if (!Array.isArray(elements)) { + return null; + } + // Filter only numeric values + const numericElements = elements.filter((item) => typeof item === "number"); + // Check if array is empty after filtering + if (numericElements.length === 0) { + return -Infinity; + } + // Return the maximum value + return Math.max(...numericElements); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..6fdbca1b9 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,7 +16,6 @@ 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"); // Given an array with one number // When passed to the max function @@ -41,3 +40,16 @@ test.todo("given an empty array, returns -Infinity"); // 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 + +describe("findMax", () => { + [ + { input: ["apple", "banana", 1], expected: 1 }, + { input: [1, -1, -100], expected: 1 }, + { input: [-10, -20, -3, -4], expected: -3 }, + { input: [1.5, 10.5, 0.5], expected: 10.5 }, + { input: ["apple", "banana", "cherry"], expected: -Infinity }, + ].forEach(({ input, expected }) => + it(`returns the maximum for [${input}]`, () => + expect(findMax(input)).toEqual(expected)) + ); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..09ac31c07 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,23 @@ function sum(elements) { + // Check if input is an array + if (!Array.isArray(elements)) { + return null; + } + // Check if array is empty + if (elements.length === 0) { + return 0; + } + // Filter only numeric values + const numericElements = elements.filter((item) => typeof item === "number"); + if (numericElements.length === 0) { + return -Infinity; + } + // Calculate the sum of numeric values + let total = 0; + for (let i = 0; i < numericElements.length; i++) { + total += numericElements[i]; + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..b697f40e5 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,7 +13,6 @@ 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") // Given an array with just one number // When passed to the sum function @@ -34,3 +33,16 @@ test.todo("given an empty array, returns 0") // 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 + +describe("sum", () => { + [ + { input: ["apple", "banana", 1, 10], expected: 11 }, + { input: [1, -1, -100], expected: -100 }, + { input: [-10, -20, -3, -4], expected: -37 }, + { input: [1.5, 10.5, 0.5], expected: 12.5 }, + { input: ["apple", "banana", "cherry"], expected: -Infinity }, + ].forEach(({ input, expected }) => + it(`returns the sum for [${input}]`, () => + expect(sum(input)).toEqual(expected)) + ); +}); 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; }