diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..17865dfac 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,15 @@ // 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 nums = list.filter((n) => typeof n === "number" && !isNaN(n)); + if (!nums.length) return null; + + const sorted = nums.slice().sort((a, b) => a - b); + const mid = Math.floor(sorted.length / 2); + + return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..bfedf4ed4 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,17 @@ -function dedupe() {} +function dedupe(arr) { + if (!Array.isArray(arr)) return []; + + const seen = new Set(); + const result = []; + + for (const item of arr) { + if (!seen.has(item)) { + seen.add(item); + result.push(item); + } + } + + return result; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..7cdc5c6bd 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,11 @@ function findMax(elements) { + if (!Array.isArray(elements)) return -Infinity; + + const nums = elements.filter((n) => typeof n === "number" && !isNaN(n)); + + if (nums.length === 0) return -Infinity; + + return nums.reduce((max, curr) => (curr > max ? curr : max), -Infinity); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..760bd1fb5 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,44 @@ 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([])).toBe(-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([8])).toBe(8); + }); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall - +test("returns the largest number from positive and negative numbers", () => { + expect(findMax([5, -9, 25, 8, -12])).toBe(25); + }); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero - +test("returns the closest to zero for all negative numbers", () => { + expect(findMax([-10, -2, -25])).toBe(-2); + }); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number - +test("returns the largest decimal number", () => { + expect(findMax([1.9, 2.9, 2.5])).toBe(2.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("ignores non-numeric values", () => { + expect(findMax(["hey", 10, "hello", 50, 20])).toBe(50); + }); // 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("returns -Infinity for only non-number values", () => { + expect(findMax(["a", null, undefined])).toBe(-Infinity); + }); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..10f1fbe0f 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,15 @@ function sum(elements) { + if (!Array.isArray(elements)) return 0; + + let total = 0; + + for (const item of elements) { + if (typeof item === "number") { + total += item; + } + } + + return total; } module.exports = sum; diff --git a/Sprint-1/package-lock.json b/Sprint-1/package-lock.json index 83e427d0b..743bde1f7 100644 --- a/Sprint-1/package-lock.json +++ b/Sprint-1/package-lock.json @@ -2125,9 +2125,9 @@ } }, "node_modules/istanbul-lib-instrument/node_modules/semver": { - "version": "7.6.3", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz", - "integrity": "sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==", + "version": "7.7.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.4.tgz", + "integrity": "sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==", "dev": true, "license": "ISC", "bin": { @@ -2664,9 +2664,9 @@ } }, "node_modules/jest-snapshot/node_modules/semver": { - "version": "7.6.3", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz", - "integrity": "sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==", + "version": "7.7.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.4.tgz", + "integrity": "sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==", "dev": true, "license": "ISC", "bin": { @@ -2898,9 +2898,9 @@ } }, "node_modules/make-dir/node_modules/semver": { - "version": "7.6.3", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz", - "integrity": "sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==", + "version": "7.7.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.4.tgz", + "integrity": "sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==", "dev": true, "license": "ISC", "bin": { 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; }