diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..206d4b7be 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,29 @@ // or contains values that aren't numbers (the function is expected to throw - see the tests). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + // validate the array + if (!Array.isArray(list)) { + throw new Error("calculateMedian requires an array of numbers"); + } + // Empty array should throw an error + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } + + const allNumbers = list.every( + (item) => typeof item === "number" && !Number.isNaN(item) + ); + if (!allNumbers) { + throw new Error("calculateMedian requires an array of numbers"); + } + const sorted = [...list].sort((a, b) => a - b); + const middleIndex = Math.floor(sorted.length / 2); + + if (sorted.length % 2 == 0) { + return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; + } else { + return sorted[middleIndex]; + } } module.exports = calculateMedian; diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 51d61cc86..ad0b85244 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -47,20 +47,14 @@ describe("calculateMedian", () => { expect(calculateMedian([6, -2, 2, 12, 14])).toEqual(6); }); - it("doesn't modify the input array [3, 1, 2]", () => { - const list = [3, 1, 2]; - calculateMedian(list); - expect(list).toEqual([3, 1, 2]); - }); - - // There is no median of an empty array, so calculateMedian should throw + // There is no median of an empty array, so calculateMedian should throw an error it("throws when given an empty array", () => { expect(() => calculateMedian([])).toThrow( new Error("calculateMedian requires a non-empty array") ); }); - // Input that isn't an array should throw + // Input that isn't an array should throw an error it("throws when given a string", () => { expect(() => calculateMedian("banana")).toThrow( new Error("calculateMedian requires an array of numbers") @@ -91,7 +85,7 @@ describe("calculateMedian", () => { ); }); - // Arrays containing any non-number value should throw, rather than filtering them out + // Arrays containing any non-number value should throw an error , rather than filtering them out it("throws for an array of strings", () => { expect(() => calculateMedian(["ten", "twenty", "thirty"])).toThrow( new Error("calculateMedian requires an array of numbers") diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..71474ddab 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(arr) { + // initialize an empty array + let dedupedArr =[]; + for( element of arr){ + if(!dedupedArr.includes(element)){ + dedupedArr.push(element); + } + + } + return dedupedArr +} +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 d7c8e3d8e..76601e0c0 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,4 +1,5 @@ const dedupe = require("./dedupe.js"); + /* Dedupe Array @@ -16,13 +17,22 @@ 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("given an empty array, it returns an empty array", ()=>{ + expect(dedupe([])).toEqual([]) +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array +test("Given an array with no duplicates, returns a copy of the original array", ()=>{ + expect(dedupe([3,4,6])).toEqual([3,4,6]) +}) // 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 // first occurrence of each element from the original array. +test('Given an array of strings or numbers, returns a new array with duplicates removed while preserving the first occurrence of each element from the original array',()=>{ + expect(dedupe(['h',4,'k','h',3,5,4])).toEqual(['h',4,'k',3,5]) +} +) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..cd2e3175e 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,22 @@ function findMax(elements) { + // checking if the input is an array + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + if (elements.length === 0) { + return -Infinity; + } + const allNumbers = elements.every( + (item) => typeof item === "number" && !Number.isNaN(item) + ); + if (!allNumbers) { + throw new Error("findMax requires an array of numbers"); + } + + const sorted = [...elements].sort((a, b) => a - b); + const max = sorted[sorted.length - 1]; + + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..d407356b9 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,50 @@ 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([22])).toBe(22); +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall - +test("Given an array with both positive and negative numbers, returns the largest number ", () => { + expect(findMax([-3, 6, -9])).toBe(6); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero - +test("Given an array with just negative numbers, returns the closest one to zero", () => { + expect(findMax([-3, -4, -1])).toBe(-1); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("Given an array with decimal numbers, returns the largest decimal number", () => { + expect(findMax([3.5, 4.5, 6.5])).toBe(6.5); +}); // Given an array containing a value that isn't a number // When passed to the max function // Then it should throw Error("findMax requires an array of numbers") +test("throws an error when given a non number value", () => { + expect(() => findMax(["3", 5, "undefined", null])).toThrow( + new Error("findMax requires an array of numbers") + ); +}); // Given something that isn't an array at all, such as "hey", 42 or no argument // When passed to the max function // Then it should throw Error("findMax requires an array of numbers") +test("throws an error when given an array with only non-number values", () => { + expect(() => findMax(["Holla!", NaN, true])).toThrow( + new Error("findMax requires an array of numbers") + ); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..0050e6b5f 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,24 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + + if (elements.length === 0) { + return 0; + } + // every element must be a valid number + const allNumbers = elements.every( + (item) => typeof item === "number" && !Number.isNaN(item) + ); + if (!allNumbers) { + throw new Error("sum requires an array of numbers"); + } + + let total = 0; + for (let i = 0; i < elements.length; i++) { + total += elements[i]; + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..ca7db09e6 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,44 @@ 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("given an empty array, returns 0", () => { + expect(sum([])).toBe(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number - +test("Given an array with just one number, return that number", () => { + expect(sum([3])).toBe(3); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("Given an array with negative numbers, return the correct total", () => { + expect(sum([-2, -4, -1])).toBe(-7); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("Given an array with decimal/float numbers, returns the correct total sum", () => { + expect(sum([3.5, 6.1])).toBe(9.6); +}); // Given an array containing a value that isn't a number // When passed to the sum function // Then it should throw Error("sum requires an array of numbers") +test("throws an error when given an array contains a non-number value", () => { + expect(() => sum(["4", "Holla", 6, 9, 1])).toThrow( + new Error("sum requires an array of numbers") + ); +}); // Given something that isn't an array at all, such as "hey", 42 or no argument // When passed to the sum function // Then it should throw Error("sum requires an array of numbers") +test("throws an error when given a non array value", () => { + expect(() => sum([null, undefined, NaN, true])).toThrow( + new Error("sum requires an array of numbers") + ); +}); diff --git a/Sprint-1/package.json b/Sprint-1/package.json index 330755010..9901d571a 100644 --- a/Sprint-1/package.json +++ b/Sprint-1/package.json @@ -11,5 +11,6 @@ "license": "ISC", "devDependencies": { "jest": "^29.7.0" - } + }, + "type": "commonjs" } diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..5d794778f 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +1,21 @@ // 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; +// function includes(list, target) { +// for (let index = 0; index < list.length; index++) { +// const element = list[index]; +// if (element === target) { +// return true; +// } +// } +// return false; +// } +function includes(list, target){ + for (element of list){ + if(element === target){ + return true } } - return false; + return false } module.exports = includes; diff --git a/package.json b/package.json new file mode 100644 index 000000000..a24609e75 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "module-data-groups", + "version": "1.0.0", + "description": "Like learning a musical instrument, programming requires daily practice.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Ebrahim-Moqbel/Module-Data-Groups.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "bugs": { + "url": "https://github.com/Ebrahim-Moqbel/Module-Data-Groups/issues" + }, + "homepage": "https://github.com/Ebrahim-Moqbel/Module-Data-Groups#readme", + "dependencies": { + "jest": "^30.5.1" + } +}