diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..4e830aae1 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,30 @@ // 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) || list.length === 0) { + return null; + } + const numbersOnly = list.filter(element => typeof element === 'number'); + if (numbersOnly.length === 0) { + return null; + } + + const sortedList = [...numbersOnly].sort((a, b) => a - b); + + if (sortedList.length % 2 === 0) { + const middleIndex = Math.floor(sortedList.length / 2); + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } + const middleIndex = Math.floor(sortedList.length / 2); + + return sortedList[middleIndex]; + + + + } + +const salaries = [10, 40, 50, 70, 90] +const median = calculateMedian(salaries); module.exports = calculateMedian; + diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..f549a8280 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -48,3 +48,4 @@ describe("calculateMedian", () => { 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..73abb63de 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,10 @@ -function dedupe() {} +function dedupe(list) { + if (!Array.isArray(list)) { + + return []; + } + + return [...new Set(list)]; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..388944344 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,7 +16,7 @@ E.g. dedupe([1, 2, 1]) returns [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.todo("given an empty array, it returns an empty array"); // Given an array with no duplicates // When passed to the dedupe function @@ -24,5 +24,31 @@ test.todo("given an empty array, it returns an empty array"); // Given an array of strings or numbers // When passed to the dedupe function -// Then it should return a new array with duplicates removed while preserving the +// Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. + + +describe("dedupe function", () => { + test("should remove duplicate elements from an array", () => { + expect(dedupe([1, 2, 2, 3, 1, 4])).toEqual([1, 2, 3, 4]); + expect(dedupe(["apple", "banana", "apple", "orange"])).toEqual([ + "apple", + "banana", + "orange", + ]); + }); + + test("should return the same array if there are no duplicates", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + }); + + test("should return an empty array if given an empty array", () => { + expect(dedupe([])).toEqual([]); + }); + + test("should return an empty array if given invalid input (non-arrays)", () => { + expect(dedupe(null)).toEqual([]); + expect(dedupe(undefined)).toEqual([]); + expect(dedupe("not an array")).toEqual([]); + }); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..c8bb074bc 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,14 @@ function findMax(elements) { + if (!Array.isArray(elements) || elements.length === 0) { + + return -Infinity; + } + const numbersOnly = elements.filter(item => typeof item === 'number' && !isNaN(item)); + if (numbersOnly.length === 0) { + + return -Infinity; + } + return Math.max(...numbersOnly); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..f8f713299 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -41,3 +41,36 @@ 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 function", () => { + test("should return the maximum number in an array of positive numbers", () => { + expect(findMax([1, 5, 3, 9, 2])).toBe(9); + }); + + test("should return the maximum number in an array with negative numbers", () => { + expect(findMax([-10, -3, -50, -1])).toBe(-1); + }); + + test("should handle arrays with mixed types and ignore non-numbers", () => { + expect(findMax([1, "apple", 5, null, true, 3])).toBe(5); + }); + + test("should ignore NaN values", () => { + expect(findMax([1, NaN, 10, 2])).toBe(10); + }); + + test("should return -Infinity if given an empty array", () => { + expect(findMax([])).toBe(-Infinity); + }); + + test("should return -Infinity if given an array with no valid numbers", () => { + expect(findMax(["a", "b", null, NaN])).toBe(-Infinity); + }); + + test("should return -Infinity for non-array inputs", () => { + expect(findMax(null)).toBe(-Infinity); + expect(findMax(undefined)).toBe(-Infinity); + expect(findMax("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..45ea726a6 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,17 @@ function sum(elements) { + if (!Array.isArray(elements) || elements.length === 0) { + return 0; + } + + const numbersOnly = elements.filter(item => typeof item === "number" && !isNaN(item)); + + if (numbersOnly.length === 0) { + return 0; + } + + return numbersOnly.reduce((acc, curr) => acc + curr, 0); + } module.exports = sum; + diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..06ab62cb6 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,7 +13,7 @@ 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.todo("given an empty array, returns 0"); // Given an array with just one number // When passed to the sum function @@ -34,3 +34,42 @@ 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 function", () => { + // Given an empty array + test("given an empty array, returns 0", () => { + expect(sum([])).toBe(0); + }); + + // Given an array with just one number + test("given an array with just one number, returns that number", () => { + expect(sum([42])).toBe(42); + }); + + // Given an array containing negative numbers + test("given an array containing negative numbers, returns the correct total sum", () => { + expect(sum([10, -5, 20, -15])).toBe(10); + }); + + // Given an array with decimal/float numbers + test("given an array with decimal/float numbers, returns the correct total sum", () => { + expect(sum([1.5, 2.25, 3.25])).toBe(7); + }); + + // Given an array containing non-number values + test("given an array containing non-number values, ignores non-numerical values", () => { + expect(sum(["hey", 10, "hi", 60, 10])).toBe(80); + expect(sum([10, true, null, undefined, NaN, 20])).toBe(30); + }); + + // Given an array with only non-number values + test("given an array with only non-number values, returns 0", () => { + expect(sum(["apple", "banana", true, null])).toBe(0); + }); + + // Edge case: Non-array inputs + test("given non-array inputs, returns 0", () => { + expect(sum(null)).toBe(0); + expect(sum("not an array")).toBe(0); + }); +}); \ No newline at end of file diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..40126317c 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,11 @@ -function contains() {} +function contains(obj, property) { + // Check that 'obj' is a valid, non-array object + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + return false; + } + + // Object.hasOwn checks if the key directly exists on the object + return Object.hasOwn(obj, property); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..897e80670 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -33,3 +33,24 @@ test.todo("contains on empty object returns false"); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error + + +describe("contains function", () => { + test("contains on empty object returns false", () => { + expect(contains({}, "a")).toBe(false); + }); + + test("returns true when property exists on the object", () => { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); + }); + + test("returns false when property does not exist on the object", () => { + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); + }); + + test("returns false when given invalid parameters like an array", () => { + expect(contains(["a", "b"], "0")).toBe(false); + expect(contains(null, "a")).toBe(false); + expect(contains("not an object", "a")).toBe(false); + }); +}); \ No newline at end of file