From 466c1c5a0c65087605b9981d9a84e050e547659c Mon Sep 17 00:00:00 2001 From: dcostaprakash Date: Fri, 13 Mar 2026 22:53:59 +0000 Subject: [PATCH] GLASGOW | Jan-26-ITP | Prakash Dcosta | Sprint 1 | Data Groups Self checklist - [X] I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title - [X] My changes meet the requirements of the task - [X] I have tested my changes - [X] My changes follow the [style guide] ## Changelist In this PR, i have implemented functions according to requrements and built test cases --- Sprint-1/fix/median.js | 26 ++++++++++++++++++++--- Sprint-1/fix/median.test.js | 34 ++++++++++++++++++++++++++----- Sprint-1/implement/dedupe.js | 5 ++++- Sprint-1/implement/dedupe.test.js | 16 ++++++++++++++- Sprint-1/implement/max.js | 9 +++++++- Sprint-1/implement/max.test.js | 22 +++++++++++++++++++- Sprint-1/implement/sum.js | 9 +++++++- Sprint-1/implement/sum.test.js | 21 ++++++++++++++++--- Sprint-1/refactor/includes.js | 3 +-- 9 files changed, 127 insertions(+), 18 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..5bbe29102 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,29 @@ // 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; + // It must be an array + if (!Array.isArray(list)) { + return null; + } + // Keep only numeric values + const numbers = list.filter( + (value) => typeof value === "number" && !isNaN(value) + ); + + // If there are no numbers existing then return null + if (numbers.length === 0) return null; + + // Sort numbers without modifying original list + const sorted = [...numbers].sort((a, b) => a - b); + const middle = Math.floor(sorted.length / 2); + + // For even length + if (sorted.length % 2 === 0) { + return (sorted[middle - 1] + sorted[middle]) / 2; + } + + // For odd length + return sorted[middle]; } module.exports = calculateMedian; diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..7753860db 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]", () => { @@ -32,9 +34,30 @@ describe("calculateMedian", () => { calculateMedian(list); expect(list).toEqual([3, 1, 2]); }); + // Test for single number array + it("returns the value for a single-element array", () => { + expect(calculateMedian([5])).toBe(5); + }); + // Test for only 1 numeric value in an array + it("returns the only numeric value in mixed array", () => { + expect(calculateMedian(["apple", 10, "banana"])).toBe(10); + }); + // Test for decimal numbers only + it("works with decimal numbers", () => { + expect(calculateMedian([2.5, 3.5, 4.5])).toBe(3.5); + }); - [ '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 invalid input (${val})`, () => + expect(calculateMedian(val)).toBe(null)) ); [ @@ -45,6 +68,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 781e8718a..ac917336c 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,4 @@ -function dedupe() {} +function dedupe(arr) { + return [...new Set(arr)]; +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..d2b644e70 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,26 @@ 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"); +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("returns original array if there are no duplicates", () => { + expect(dedupe([10, 45, 85, 20])).toEqual([10, 45, 85, 20]); + expect(dedupe(["hello", "a", "hi", "b"])).toEqual(["hello", "a", "hi", "b"]); +}); // 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 +test("returns first occurance for array of strings or numbers", () => { + expect(dedupe([10, 10, 20, 20, 30, 40])).toEqual([10, 20, 30, 40]); + expect(dedupe(["hello", "hello", "hi", "a", "hi"])).toEqual([ + "hello", + "hi", + "a", + ]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..fb88ac9cf 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,11 @@ -function findMax(elements) { +function findMax(arr) { + let max = -Infinity; + for (let i = 0; i < arr.length; i++) { + if (typeof arr[i] === "number" && arr[i] > max) { + max = arr[i]; + } + } + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..d5574f78d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,48 @@ 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([5])).toBe(5); +}); // 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 overall", () => { + expect(findMax([-25, 3, 12, -32])).toBe(12); +}); // 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 number to zero", () => { + expect(findMax([-25, -345, -1, -72])).toBe(-1); +}); // 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.85, 2.563, 12.23, 0.24])).toBe(12.23); +}); // 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("returnes max and ignores non numeric values", () => { + expect(findMax(["stay", 3, "quiet", -32, 5])).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 +test("returns infinity in non number values", () => { + expect(findMax(["hi", "sally", "a", "alpha"])).toBe(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..7801c6e3a 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,11 @@ -function sum(elements) { +function sum(arr) { + let total = 0; + for (let i = 0; i < arr.length; i++) { + if (typeof arr[i] === "number") { + total += arr[i]; + } + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..755faddf2 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, 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("returns the same number when an array has just 1 number", () => { + expect(sum([8])).toBe(8); +}); // 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, returns correct sum", () => { + expect(sum([-50, -20, 10])).toBe(-60); +}); // 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 correct sum", () => { + expect(sum([2.8, 4.2, 7.7])).toBe(14.7); +}); // 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 with non number values, returns sum of numbers", () => { + expect(sum(["quiet", -20, "good", 10, 25])).toBe(15); +}); // 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("returns 0 foe non number values", () => { + expect(sum(["sally", "cousin", "hi", "a"])).toBe(0); +}); 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; }