diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..a2a10dd9d 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,8 +6,19 @@ // 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]; + if (!Array.isArray(list)) return null; + + const numbers = list.filter(val => typeof val === 'number'); + if (numbers.length === 0) return null; + numbers.sort((a, b) => a - b); + + if (numbers.length % 2 === 0) { + const mid1 = numbers[numbers.length / 2 - 1]; + const mid2 = numbers[numbers.length / 2]; + return (mid1 + mid2) / 2; + } + const middleIndex = Math.floor(numbers.length / 2); + const median = numbers[middleIndex]; return median; } diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..d43d1c419 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,10 @@ -function dedupe() {} +function dedupe(list) { + let listWithoutDuplicates = []; + for (let i = 0; i < list.length; i++) { + if(listWithoutDuplicates.includes(list[i]) === false) { + listWithoutDuplicates.push(list[i]); + } + } + return listWithoutDuplicates; +} +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..c1cb9512d 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -25,3 +25,19 @@ 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", () => { + it("given an empty array, it returns an empty array", () => { + expect(dedupe([])).toEqual([]); + }); + + it("given an array with no duplicates, it returns a copy of the original array", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + }); + + it("given an array with strings or numbers, it removes the duplicate values, preserving the first occurrence of each element", () => { + expect(dedupe(['a','a','a','b','b','c'])).toEqual(['a','b','c']); + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + expect(dedupe([1, 2, 1])).toEqual([1, 2]); + }); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..d4e02ce5f 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,11 @@ function findMax(elements) { +let max = -Infinity; +for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number" && elements[i] > max) { + max = elements[i]; + } +} +return max; } -module.exports = findMax; +module.exports = findMax; \ No newline at end of file diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..49ba64599 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -41,3 +41,28 @@ 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("max", () => { + test("given an empty array, returns -Infinity", () => { + expect(findMax([])).toBe(-Infinity); + }); + + test("given an array with one number, returns that number", () => { + expect(findMax([42])).toBe(42); + }); + test("given an array with both positive and negative numbers, returns the largest number overall", () => { + expect(findMax([-10, 0, 5, -20, 15])).toBe(15); + }); + test("given an array with just negative numbers, returns the closest one to zero", () => { + expect(findMax([-50, -20, -5, -1])).toBe(-1); + }); + test("given an array with decimal numbers, returns the largest decimal number", () => { + expect(findMax([1.5, 2.3, 0.7, 3.8])).toBe(3.8); + }); + test("given an array with non-number values, returns the max and ignores non-numeric values", () => { + expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60); + }); + test("given an array with only non-number values, returns the least surprising value given how it behaves for all other inputs", () => { + expect(findMax(['hey', 'hi', '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..13d33c496 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,11 @@ function sum(elements) { + let total = 0; + for (let element of elements) { + if (typeof element === 'number') { + total += element; + } + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..62dea20e9 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -34,3 +34,28 @@ 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", () => { + test("given an empty array, returns 0", () => { + expect(sum([])).toBe(0); + }); + + test("given an array with just one number, returns that number", () => { + expect(sum([5])).toBe(5); + }); + + test("given an array containing negative numbers, returns the correct total sum", () => { + expect(sum([-1, -2, -3])).toBe(-6); + }); + + test("given an array with decimal/float numbers, returns the correct total sum", () => { + expect(sum([1.5, 2.5, 3.0])).toBe(7.0); + }); + + test("given an array containing non-number values, ignores them and returns the sum of the numerical elements", () => { + expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80); + }); + + test("given an array with only non-number values, returns 0", () => { + expect(sum(['hey', 'hi', 'hello'])).toBe(0); + }); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..4a23cb6d2 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,5 @@ -// 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; } @@ -10,4 +7,4 @@ function includes(list, target) { return false; } -module.exports = includes; +module.exports = includes; \ No newline at end of file