From dc8fa61604ea585453f3ecd8869a72ab9286549e Mon Sep 17 00:00:00 2001 From: Rizqah Date: Wed, 22 Jul 2026 00:04:21 +0100 Subject: [PATCH 1/2] all tasks completed for data groups sprint 1 --- Sprint-1/fix/median.js | 20 ++++++++++++++++++-- Sprint-1/implement/dedupe.js | 13 ++++++++++++- Sprint-1/implement/dedupe.test.js | 14 +++++++++++++- Sprint-1/implement/max.js | 11 ++++++++++- Sprint-1/implement/max.test.js | 30 ++++++++++++++++++++++++------ Sprint-1/implement/sum.js | 14 ++++++++++++++ Sprint-1/implement/sum.test.js | 25 ++++++++++++++++++++----- Sprint-1/refactor/includes.js | 9 ++++++--- 8 files changed, 117 insertions(+), 19 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..b45de3759 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]; + if (!Array.isArray(list)) { + return null; +} + const list2 = list.filter(item => typeof item === "number").sort( (a,b) => (a - b)); + if (list2.length === 0) { + return null; + } + if(list2.length % 2 === 1){ + const middleIndex = Math.floor(list2.length / 2); + const median = list2[middleIndex]; + return median; + } + if(list2.length % 2 === 0){ + const middleIndex = Math.floor(list2.length / 2); + const median = (list2[middleIndex] + list2[middleIndex - 1]) / 2; return median; + } + } + module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..99e55c070 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(input) { +const newArray = [] + if(input.length === 0){ + return [] + } + for(const i in input) + for(const j in input) + if (i !== j && !newArray.includes(input[i])) + newArray.push(input[i]) + return newArray; +} +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..064296df1 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,25 @@ 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, it should return the same array`, () => { + expect(dedupe(['a','b','c'])).toEqual(['a','b','c']); + expect(dedupe(['1','2','3'])).toEqual(['1','2','3']); + +}); // 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 with duplicates, it should return the array with duplicates removed preserving the first occurrence`, () => { + expect(dedupe(['b','c','a','b','c', 'b','c'])).toEqual(['b','c','a']); + expect(dedupe(['1','2','2','3','1','2','3'])).toEqual(['1','2','3']); + +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..13e55484b 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,13 @@ function findMax(elements) { + if (elements.length === 0){ + return -Infinity; +} +const cleanElements = elements.filter(item => typeof item === "number").sort( (a,b) => (a - b)); + if (cleanElements.length === 0){ + return -Infinity; + } + else{ + return cleanElements[cleanElements.length-1] + } } - module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..c72c7659d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +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"); +test(`given an empty array, it returns -Infinity`, () => { + expect(findMax([])).toEqual(-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, it should return that number`, () => { + expect(findMax([80])).toEqual(80); + expect(findMax([1])).toEqual(1); +}); // 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 numbers and negative numbers, it should return the largest number overall`, () => { + expect(findMax([80, -30, 50, -10, 20, 100, 40])).toEqual(100); + expect(findMax([-90, 30, 250, 1, 120, -100, -40])).toEqual(250); +}); // 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 negative numbers, it should return the addition of them all`, () => { + expect(findMax([-80, -30, -50, -10, -20, -100, -40])).toEqual(-10); +}); // 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, it should return the largest decimal number`, () => { + expect(findMax([-80.5, -30.1, 50.7, -10, 20.4, 10.3, 60.1])).toEqual(60.1); +}); // 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(`given an array of numeric and non-numeric values, it should return the largest numeric`, () => { + expect(findMax(['hey', 10, 'hi', 60, 10])).toEqual(60); + expect(findMax(['i', 'love', 10, 'code', 'hi', 80, 60, 'your', 'future', 10])).toEqual(80); +}); // 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(`given an array of non-numeric values, it should return -Infinity`, () => { + expect(findMax(['i', 'love', 'code', 'your', 'future'])).toEqual(-Infinity); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..323725572 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,18 @@ function sum(elements) { + if (elements.length === 0){ + return 0; } +const cleanElements = elements.filter(item => typeof item === "number").sort( (a,b) => (a - b)); +let sum = 0 +if (cleanElements.length === 0){ + return 0; + } + else{ + for(let i = 0; i < cleanElements.length; i++) + sum += cleanElements[i] + + return sum; + } +} module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..999675cea 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,39 @@ 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, it returns zero`, () => { + expect(sum([])).toEqual(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 one number, it should return that number`, () => { + expect(sum([80])).toEqual(80); + expect(sum([1])).toEqual(1); +}); // 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, it should return the addition of them all`, () => { + expect(sum([-80, -30, -50, -10, -20, -100, -40])).toEqual(-330); +}); // 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 numbers, it should return the total`, () => { + expect(sum([-80.5, -30.1, 50.7, -10, 20.4, 10.3, 60.1])).toEqual(20.9); +}); // Given an array containing non-number values // When passed to the sum function // Then it should ignore the non-numerical values and return the sum of the numerical elements - +test(`given an array of numeric and non-numeric values, it should return the total of the numeric values`, () => { + expect(sum(['hey', 10, 'hi', 60, 10])).toEqual(80); + expect(sum(['i', 'love', 10, 'code', 'hi', 80, 60, 'your', 'future', 10])).toEqual(160); +}); // 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 +test(`given an array of non-numeric values, it should return zero`, () => { + expect(sum(['i', 'love', 'code', 'your', 'future'])).toEqual(0); +}) \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..98ca0ff33 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +1,16 @@ // 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 item of list) { + const element = item; if (element === target) { return true; } } return false; } +console.log(includes(["a", "b", "c", "d"], "c")) -module.exports = includes; + + +module.exports = includes; \ No newline at end of file From 94985df76932f0d8e958b4765b669613dd681711 Mon Sep 17 00:00:00 2001 From: Rizqah Date: Wed, 22 Jul 2026 12:05:32 +0100 Subject: [PATCH 2/2] auto formatted properly --- Sprint-1/fix/median.js | 26 +++++++++++++------------- Sprint-1/fix/median.test.js | 22 +++++++++++++++++----- Sprint-1/implement/dedupe.js | 19 +++++++++---------- Sprint-1/implement/dedupe.test.js | 12 +++++------- Sprint-1/implement/max.js | 21 +++++++++++---------- Sprint-1/implement/max.test.js | 10 ++++++---- Sprint-1/implement/sum.js | 27 +++++++++++++-------------- Sprint-1/implement/sum.test.js | 10 ++++++---- Sprint-1/refactor/includes.js | 6 ++---- 9 files changed, 82 insertions(+), 71 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b45de3759..aecfa710c 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -7,24 +7,24 @@ function calculateMedian(list) { if (!Array.isArray(list)) { - return null; -} - const list2 = list.filter(item => typeof item === "number").sort( (a,b) => (a - b)); + return null; + } + const list2 = list + .filter((item) => typeof item === "number") + .sort((a, b) => a - b); if (list2.length === 0) { return null; } - if(list2.length % 2 === 1){ - const middleIndex = Math.floor(list2.length / 2); - const median = list2[middleIndex]; - return median; + if (list2.length % 2 === 1) { + const middleIndex = Math.floor(list2.length / 2); + const median = list2[middleIndex]; + return median; } - if(list2.length % 2 === 0){ - const middleIndex = Math.floor(list2.length / 2); - const median = (list2[middleIndex] + list2[middleIndex - 1]) / 2; - return median; + if (list2.length % 2 === 0) { + const middleIndex = Math.floor(list2.length / 2); + const median = (list2[middleIndex] + list2[middleIndex - 1]) / 2; + return median; } - } - module.exports = calculateMedian; diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..c262c3776 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -13,7 +13,8 @@ describe("calculateMedian", () => { { input: [1, 2, 3, 4], expected: 2.5 }, { input: [1, 2, 3, 4, 5, 6], expected: 3.5 }, ].forEach(({ input, expected }) => - it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + it(`returns the median for [${input}]`, () => + expect(calculateMedian(input)).toEqual(expected)) ); [ @@ -24,7 +25,8 @@ describe("calculateMedian", () => { { input: [110, 20, 0], expected: 20 }, { input: [6, -2, 2, 12, 14], expected: 6 }, ].forEach(({ input, expected }) => - it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + it(`returns the correct median for unsorted array [${input}]`, () => + expect(calculateMedian(input)).toEqual(expected)) ); it("doesn't modify the input array [3, 1, 2]", () => { @@ -33,8 +35,17 @@ describe("calculateMedian", () => { expect(list).toEqual([3, 1, 2]); }); - [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => - it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) + [ + "not an array", + 123, + null, + undefined, + {}, + [], + ["apple", null, undefined], + ].forEach((val) => + it(`returns null for non-numeric array (${val})`, () => + expect(calculateMedian(val)).toBe(null)) ); [ @@ -45,6 +56,7 @@ describe("calculateMedian", () => { { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 }, { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 }, ].forEach(({ input, expected }) => - it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + 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 99e55c070..82144c506 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,12 +1,11 @@ function dedupe(input) { -const newArray = [] - if(input.length === 0){ - return [] - } - for(const i in input) - for(const j in input) - if (i !== j && !newArray.includes(input[i])) - newArray.push(input[i]) - return newArray; + const newArray = []; + if (input.length === 0) { + return []; + } + for (const i in input) + for (const j in input) + if (i !== j && !newArray.includes(input[i])) newArray.push(input[i]); + return newArray; } -module.exports = dedupe; \ No newline at end of file +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 064296df1..2b3b03f2c 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -24,17 +24,15 @@ test(`given an empty array, it returns an empty array`, () => { // When passed to the dedupe function // Then it should return a copy of the original array test(`given an array with no duplicates, it should return the same array`, () => { - expect(dedupe(['a','b','c'])).toEqual(['a','b','c']); - expect(dedupe(['1','2','3'])).toEqual(['1','2','3']); - + expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]); + expect(dedupe(["1", "2", "3"])).toEqual(["1", "2", "3"]); }); // 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. test(`given an array with duplicates, it should return the array with duplicates removed preserving the first occurrence`, () => { - expect(dedupe(['b','c','a','b','c', 'b','c'])).toEqual(['b','c','a']); - expect(dedupe(['1','2','2','3','1','2','3'])).toEqual(['1','2','3']); - + expect(dedupe(["b", "c", "a", "b", "c", "b", "c"])).toEqual(["b", "c", "a"]); + expect(dedupe(["1", "2", "2", "3", "1", "2", "3"])).toEqual(["1", "2", "3"]); }); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 13e55484b..f16375fee 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,13 +1,14 @@ function findMax(elements) { - if (elements.length === 0){ - return -Infinity; -} -const cleanElements = elements.filter(item => typeof item === "number").sort( (a,b) => (a - b)); - if (cleanElements.length === 0){ - return -Infinity; - } - else{ - return cleanElements[cleanElements.length-1] - } + if (elements.length === 0) { + return -Infinity; + } + const cleanElements = elements + .filter((item) => typeof item === "number") + .sort((a, b) => a - b); + if (cleanElements.length === 0) { + return -Infinity; + } else { + return cleanElements[cleanElements.length - 1]; + } } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index c72c7659d..8cc0c1e77 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -50,12 +50,14 @@ test(`given an array with decimal numbers, it should return the largest decimal // When passed to the max function // Then it should return the max and ignore non-numeric values test(`given an array of numeric and non-numeric values, it should return the largest numeric`, () => { - expect(findMax(['hey', 10, 'hi', 60, 10])).toEqual(60); - expect(findMax(['i', 'love', 10, 'code', 'hi', 80, 60, 'your', 'future', 10])).toEqual(80); + expect(findMax(["hey", 10, "hi", 60, 10])).toEqual(60); + expect( + findMax(["i", "love", 10, "code", "hi", 80, 60, "your", "future", 10]) + ).toEqual(80); }); // 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(`given an array of non-numeric values, it should return -Infinity`, () => { - expect(findMax(['i', 'love', 'code', 'your', 'future'])).toEqual(-Infinity); -}); \ No newline at end of file + expect(findMax(["i", "love", "code", "your", "future"])).toEqual(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 323725572..b36e2eb9f 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,18 +1,17 @@ function sum(elements) { - if (elements.length === 0){ - return 0; -} -const cleanElements = elements.filter(item => typeof item === "number").sort( (a,b) => (a - b)); -let sum = 0 -if (cleanElements.length === 0){ - return 0; - } - else{ - for(let i = 0; i < cleanElements.length; i++) - sum += cleanElements[i] - - return sum; + if (elements.length === 0) { + return 0; + } + const cleanElements = elements + .filter((item) => typeof item === "number") + .sort((a, b) => a - b); + let sum = 0; + if (cleanElements.length === 0) { + return 0; + } else { + for (let i = 0; i < cleanElements.length; i++) sum += cleanElements[i]; - } + return sum; + } } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 999675cea..f8fdd0723 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -40,12 +40,14 @@ test(`given an array with decimal numbers, it should return the total`, () => { // When passed to the sum function // Then it should ignore the non-numerical values and return the sum of the numerical elements test(`given an array of numeric and non-numeric values, it should return the total of the numeric values`, () => { - expect(sum(['hey', 10, 'hi', 60, 10])).toEqual(80); - expect(sum(['i', 'love', 10, 'code', 'hi', 80, 60, 'your', 'future', 10])).toEqual(160); + expect(sum(["hey", 10, "hi", 60, 10])).toEqual(80); + expect( + sum(["i", "love", 10, "code", "hi", 80, 60, "your", "future", 10]) + ).toEqual(160); }); // 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 test(`given an array of non-numeric values, it should return zero`, () => { - expect(sum(['i', 'love', 'code', 'your', 'future'])).toEqual(0); -}) \ No newline at end of file + expect(sum(["i", "love", "code", "your", "future"])).toEqual(0); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 98ca0ff33..5794d749b 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -9,8 +9,6 @@ function includes(list, target) { } return false; } -console.log(includes(["a", "b", "c", "d"], "c")) +console.log(includes(["a", "b", "c", "d"], "c")); - - -module.exports = includes; \ No newline at end of file +module.exports = includes;