diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..9a7c7ac40 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,25 @@ // 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 numericList = list.filter( + (item) => typeof item === "number" && !isNaN(item) + ); + + if (numericList.length === 0) return null; + + + + numericList.sort((a, b) => a - b); + if (numericList.length % 2 === 0) { + let middleIndex = numericList.length / 2; + return (numericList[middleIndex] + numericList[middleIndex - 1]) / 2; + } else { + const middleIndex = Math.floor(numericList.length / 2); + const median = numericList[middleIndex]; + return median; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..1d136d0fc 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,8 @@ -function dedupe() {} + + function dedupe(elements) { + return [...new Set(elements)]; + } + + + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..76724579a 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,7 +16,41 @@ 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"); +describe("Dedupe", () => { + it("returns an empty array when it given an empty array", () => { + expect(dedupe([])).toEqual([]); + }); + [ + { input: [10, 12, 13], expected: [10, 12, 13] }, + { + input: ["a", "b", "keke", 1, 21, 3], + expected: ["a", "b", "keke", 1, 21, 3], + }, + { input: [null, undefined, 1, 3], expected: [null, undefined, 1, 3] }, + ].forEach(({ input, expected }) => + it(`returns a copy of the original array when given an array with no duplicates, for [${input}]`, () => + expect(dedupe(input)).toEqual(expected)) + ); + [ + { input: [10, 12, 10, 13], expected: [10, 12, 13] }, + { input: [1, "a", "a", "b", 1, 1, "a"], expected: [1, "a", "b"] }, + { + input: [null, undefined, 1, 3, 1, 1, 1, 1], + expected: [null, undefined, 1, 3], + }, + ].forEach(({ input, expected }) => { + it(`remove duplicates from [${input}]`, () => expect(dedupe(input)).toEqual(expected)); + }); + it("treats different types separately", () => { + expect(dedupe([1, "1", true, "true", false, 0])).toEqual([1, "1", true, "true", false, 0]); + }); + it("Test if the input array and result array are different",()=>{ + const input=[1,2,3,3,2,1] + const result=dedupe(input) + expect(input).not.toBe(result) + expect(result).toEqual([1,2,3]) + }) +}); // Given an array with no duplicates // When passed to the dedupe function diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..9cc876839 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,18 @@ function findMax(elements) { + if (!Array.isArray(elements)) return -Infinity; + const elementsClone = [...elements]; + + const numericElements = elementsClone.filter( + (item) => typeof item === "number" && !isNaN(item) + ); + if (numericElements.length === 0) return -Infinity; + + let max = numericElements[0]; + for (let i = 0; i < numericElements.length; i++) { + if (max < numericElements[i]) max = numericElements[i]; + } + + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..7c53f16c6 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,7 +16,46 @@ 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"); +describe("findMax", () => { + it("return -Infinity when given an empty array", () => { + expect(findMax([])).toEqual(-Infinity); + }); + + [ + { input: [0], expected: 0 }, + { input: [-12], expected: -12 }, + { input: [4], expected: 4 }, + ].forEach(({ input, expected }) => + it(`return the same number when the array has one element, for array[${input}] `, () => + expect(findMax(input)).toEqual(expected)) + ); + [ + { input: [10, 11, 13, 3, 4], expected: 13 }, + { input: [99, 3, 10, 11], expected: 99 }, + { input: [-12, -33, -10, -111], expected: -10 }, + { input: [-1, -11, -33], expected: -1 }, + { input: [-99, -2, 0, 0, -2], expected: 0 }, + ].forEach(({ input, expected }) => + it(`return the largest number for array containing, positive, negative, or mixed values , for array [${input}]`, () => + expect(findMax(input)).toEqual(expected)) + ); + [ + { input: [11.1, 14.1, 17.223, 16], expected: 17.223 }, + { input: [11.1, 11.33, 11.34, 11.2999], expected: 11.34 }, + ].forEach(({ input, expected }) => + it(`give an array with decimal numbers, return the largest decimal numbers for array [${input}]`, () => + expect(findMax(input)).toEqual(expected)) + ); + + it(`given an array with non-number values , return the largest after ignore the no-number values for an array input:["a",2,null,undefined,13,18]`, () => { + expect(findMax(["a", 2, null, undefined, 13, 18])).toEqual(18); + }); + it(`given an array with only non-number values , return -infinity`, () => { + expect(findMax(["a", "2", null, undefined, "ahmad", "Hmedan"])).toEqual( + -Infinity + ); + }); +}); // Given an array with one number // When passed to the max function diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..2b659ad5a 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,12 @@ function sum(elements) { + if (!Array.isArray(elements)) return 0; + const numericElements = elements.filter(Number.isFinite) + if (numericElements.length === 0) return 0; + let sum = 0; + for (let i = 0; i < numericElements.length; i++) { + sum += numericElements[i]; + } + return sum; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..a3e0ec7d1 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,7 +13,45 @@ 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") +describe("sum", () => { + it("return 0 when given an empty array", () => { + expect(sum([])).toBe(0); + }); + + [ + { input: [0], expected: 0 }, + { input: [-12], expected: -12 }, + { input: [4], expected: 4 }, + ].forEach(({ input, expected }) => + it(`return the same number when the array has one element, for array[${input}] `, () => + expect(sum(input)).toBe(expected)) + ); + [ + { input: [10, 11, 13, 3, 4], expected: 41 }, + { input: [99, 3, 10, 11], expected: 123 }, + { input: [-12, -33, -10, -111], expected: -166 }, + { input: [-1, -11, -33], expected: -45 }, + { input: [-99, -2, 0, 0, -2], expected: -103 }, + ].forEach(({ input, expected }) => + it(`return the sum of numbers for array containing, positive, negative, or mixed values , for array [${input}]`, () => + expect(sum(input)).toBe(expected)) + ); + + [ + { input: [11.1, 14.1, 17.223, 16], expected: 58.423 }, + { input: [11.1, 11.33, 11.34, 11.2999], expected: 45.0699 }, + { input: [1.2, 0.6, 0.005], expected: 1.805 }, + ].forEach(({ input, expected }) => + it(`returns the sum for decimal values for array [${input}]`, () => + expect(sum(input)).toBeCloseTo(expected)) + ); + it("ignores non-numeric values and returns the sum of numeric ones", () => { + expect(sum(["2", 2, null, undefined, 13, 18])).toBe(33); + }); + it("returns 0 when no numeric values are present", () => { + expect(sum(["a", "2", null, undefined, "ahmad", "Hmedan"])).toBe(0); + }); +}); // Given an array with just one number // When passed to the sum function diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..aead3967a 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +1,9 @@ // 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]; - if (element === target) { - return true; - } + for (const element of list) { + if (element === target) return true; } return false; } - module.exports = includes;