From 22a1f5476ac638c7a649f7befac07569cef71c3b Mon Sep 17 00:00:00 2001 From: Shayida Date: Sat, 15 Nov 2025 14:09:04 +0000 Subject: [PATCH 1/2] git commit -m "Finished all tasks for Sprint-1" --- Sprint-1/fix/median.js | 27 ++++++++++-- Sprint-1/fix/median.test.js | 70 +++++++++++++++---------------- Sprint-1/implement/dedupe.js | 19 ++++++++- Sprint-1/implement/dedupe.test.js | 22 +++++++++- Sprint-1/implement/max.js | 17 +++++++- Sprint-1/implement/max.test.js | 28 ++++++++++++- Sprint-1/implement/sum.js | 22 +++++++++- Sprint-1/implement/sum.test.js | 45 +++++++++++++++++++- Sprint-1/refactor/includes.js | 9 ++-- 9 files changed, 209 insertions(+), 50 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..89143a616 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,10 +5,31 @@ // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) // 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; +//} + 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(x => typeof x ==="number"); + + if(nums.length ===0) return null; + + nums.sort((a, b) => a - b); + + const mid = Math.floor(nums.length / 2); + + if (nums.length % 2 ===1){ + + return nums[mid]; + } + else{ + return (nums[mid - 1] + nums[mid]) / 2; + } } module.exports = calculateMedian; diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..c5892b3a2 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -7,44 +7,44 @@ const calculateMedian = require("./median.js"); describe("calculateMedian", () => { - [ - { input: [1, 2, 3], expected: 2 }, - { input: [1, 2, 3, 4, 5], expected: 3 }, - { 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 median for sorted arrays", () =>{ + expect(calculateMedian([1, 2, 3])).toBe(2); + expect(calculateMedian([1, 2, 3, 4, 5])).toBe(3); + expect(calculateMedian([1, 2, 3, 4])).toBe(2.5); + expect(calculateMedian([1, 2, 3, 4, 5, 6])).toBe(3.5); + }); - [ - { input: [3, 1, 2], expected: 2 }, - { input: [5, 1, 3, 4, 2], expected: 3 }, - { input: [4, 2, 1, 3], expected: 2.5 }, - { input: [6, 1, 5, 3, 2, 4], expected: 3.5 }, - { 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 median for unsorted arrays", () =>{ + expect(calculateMedian([3, 1, 2])).toBe(2); + expect(calculateMedian([5, 1, 3, 4, 2])).toBe(3); + expect(calculateMedian([4, 2, 1, 3])).toBe(2.5); + expect(calculateMedian([6, 1, 5, 3, 2, 4])).toBe(3.5); + expect(calculateMedian([110, 20, 0])).toBe(20); + expect(calculateMedian([6, -2, 2, 12, 14])).toBe(6); + }); - it("doesn't modify the input array [3, 1, 2]", () => { - const list = [3, 1, 2]; - calculateMedian(list); - expect(list).toEqual([3, 1, 2]); + it("does not modify the original array", () => { + const arr = [3, 1, 2]; + calculateMedian(arr); + expect(arr).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)) - ); + it("returns null for invalid or empty arrays", () =>{ + expect(calculateMedian("not an array")).toBe(null); + expect(calculateMedian(123)).toBe(null); + expect(calculateMedian(null)).toBe(null); + expect(calculateMedian(undefined)).toBe(null); + expect(calculateMedian({})).toBe(null); + expect(calculateMedian([])).toBe(null); + expect(calculateMedian(["apple", null, undefined])).toBe(null); + }); - [ - { input: [1, 2, "3", null, undefined, 4], expected: 2 }, - { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 }, - { input: [1, "2", 3, "4", 5], expected: 3 }, - { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 }, - { 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-numbers and returns correct median", () =>{ + expect(calculateMedian([1, 2, "3", null, undefined, 4])).toBe(2); + expect(calculateMedian(["apple", 1, 2, 3, "banana", 4])).toBe(2.5); + expect(calculateMedian([1, "2", 3, "4", 5])).toBe(3); + expect(calculateMedian([1, "apple", 2, null, 3, undefined, 4])).toBe(2.5); + expect(calculateMedian([3, "apple", 1, null, 2, undefined, 4])).toBe(2.5); + expect(calculateMedian(["banana", 5, 3, "apple", 1, 4, 2])).toBe(3); + }); }); diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..a1fa8297a 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,18 @@ -function dedupe() {} +function dedupe(arr){ + + if (!Array.isArray(arr) || arr.length === 0) return []; + + const result = []; + + for (let i = 0; i < arr.length; i++){ + + if (!result.includes(arr[i])){ + + result.push(arr[i]); + } + } + + return result; + +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..f3aae7018 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,32 @@ 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 function", () =>{ + it("returns an empty array for 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 +it("returns the same array if there are no duplicates", () =>{ + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + + expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]); + + }); // 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 + +it("removes duplicates and preserves first occurrence", () =>{ + + expect(dedupe([1, 2, 1])).toEqual([1, 2]); + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + expect(dedupe(["a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + + }); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..90f08f047 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,19 @@ function findMax(elements) { -} + if (!Array.isArray(elements)) return -Infinity; + + const numbers = elements.filter(x => typeof x ==="number"); + if (numbers.length === 0) return -Infinity; + + let max = numbers[0]; + for (let i = 1; i < numbers.length; i++) { + if (numbers[i] > max) max = numbers[i]; + } + + return max; + } + + console.log(findMax([10, 5, 30, 2])); + console.log(findMax(['a', 10, 5])); + console.log(findMax([])); module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..00487775d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,52 @@ 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 function", () => { + // empty array + it("returns -Infinity for an empty array", () => { + expect(findMax([])).toBe(-Infinity); + }); // Given an array with one number // When passed to the max function // Then it should return that number +it("returns that number for a single-element array", () => { + expect(findMax([42])).toBe(42); + }); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +it("returns the largest number overall", () =>{ + expect(findMax([-1, 0, 5, -10, 3])).toBe(5); + }); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +it("returns the closest to zero for all negative numbers", () =>{ + expect(findMax([-10, -3, -7])).toBe(-3); + }); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +it("returns the largest decimal number", () => { + expect(findMax([1.1, 2.2, 1.5])).toBe(2.2); + }); // Given an array with non-number values // When passed to the max function // Then it should return the max and ignore non-numeric values +it("ignores non-numbers and returns the max", () => { + expect(findMax([1, "a", 5, null, 2])).toBe(5); + }); // 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 + +it("returns -Infinity if no numbers are present", () => { + expect(findMax(["a", null, "b"])).toBe(-Infinity); + }); + +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..a0ede4916 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,22 @@ -function sum(elements) { -} +function sum(elements){ + + if (!Array.isArray(elements) || elements.length ===0) return 0; + + const numbers = elements.filter(x => typeof x === "number"); + + let total =0; + + for (let i =0; i< numbers.length; i++){ + + total += numbers[i]; + + } + + return total; + } + + console.log(sum([10, 20, 30])); + console.log(sum([1, "a", 2, null, 3])); + console.log(sum([])); module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..6b21f4aee 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,67 @@ 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 function", ()=>{ + it("returns 0 for an empty array", () =>{ + + expect(sum([])).toBe(0); + + }); // Given an array with just one number // When passed to the sum function // Then it should return that number +it("returns that number for a single-element array", ()=>{ + + expect(sum([42])).toBe(42); + + }); + // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +it("sums negative numbers correctly", ()=>{ + + expect(sum([-1, -2, -3])).toBe(-6); + + expect(sum([5, -3, 2])).toBe(4); + + }); + // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +it("sums decimal numbers correctly", () =>{ + + expect(sum([1.5, 2.5, 3])).toBe(7); + + expect(sum([0.1, 0.2, 0.3])).toBeCloseTo(0.6); + + }); + // 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 +it("ignores non-numbers and sums only numerical elements", () =>{ + + expect(sum(["hi", 10, "hey", 20])).toBe(30); + + expect(sum([1, "apple", 2, null, 3])).toBe(6); + + }); + // 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 + +it("returns 0 if there are no numerical elements", () =>{ + + expect(sum(["apple", null, undefined, "banana"])).toBe(0); + + }); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..376ebf49e 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,9 +1,10 @@ // 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) { +function includes(list, target){ + + for (const element of list){ + + if (element === target){ return true; } } From b3c3e350072fad2dcffe1d0074b50b4f59e88c24 Mon Sep 17 00:00:00 2001 From: Shayida Date: Sat, 15 Nov 2025 14:09:53 +0000 Subject: [PATCH 2/2] missed file --- Sprint-1/implement/dedupe.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index a1fa8297a..a546d39b8 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -13,6 +13,6 @@ function dedupe(arr){ } return result; - + } module.exports = dedupe;