From 268ee04cb8815cf8614fdef72577d933de94ad84 Mon Sep 17 00:00:00 2001 From: Mahmoud Shaabo Date: Sun, 15 Mar 2026 06:47:10 +0000 Subject: [PATCH 1/2] Complete Sprint-1: Fix median, Implement max/sum/dedupe, and Refactor includes --- Sprint-1/fix/median.js | 32 +++++++++++++++++++++--- Sprint-1/implement/dedupe.js | 13 +++++++++- Sprint-1/implement/dedupe.test.js | 41 +++++++++++++++++++------------ Sprint-1/implement/max.js | 14 +++++++++++ Sprint-1/implement/max.test.js | 37 +++++++++++++++++----------- Sprint-1/implement/sum.js | 15 +++++++++++ Sprint-1/implement/sum.test.js | 31 ++++++++++++++--------- Sprint-1/refactor/includes.js | 11 ++++++--- 8 files changed, 144 insertions(+), 50 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..22e714bc0 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,35 @@ // 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; + // 1. Guard clause: Check if the input is an array + if (!Array.isArray(list)) { + return null; + } + + // 2. Filter the array to keep only numbers + const numbersOnly = list.filter( + (item) => typeof item === "number" && !isNaN(item) + ); + + // 3. Check for an empty array + if (numbersOnly.length === 0) { + return null; + } + + // 4. Sort the numbers in ascending order + numbersOnly.sort((a, b) => a - b); + + // 5. Calculate the middle index + const middleIndex = Math.floor(numbersOnly.length / 2); + + // 6. Check if the length is even or odd + if (numbersOnly.length % 2 === 0) { + // The length is EVEN + return (numbersOnly[middleIndex - 1] + numbersOnly[middleIndex]) / 2; + } else { + // The length is ODD + return numbersOnly[middleIndex]; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..f18461e80 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +// dedupe.js +// Remove duplicate values from an array, preserving the first occurrence of each element + +function dedupe(arr) { + // Return empty array if input is empty + + // Use Set to automatically remove duplicates, then spread back into an array + // Set keeps only unique values in insertion order + 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..cd53f7963 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,27 +1,36 @@ const dedupe = require("./dedupe.js"); -/* -Dedupe Array - -📖 Dedupe means **deduplicate** - -In this kata, you will need to deduplicate the elements of an array - -E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c'] -E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8] -E.g. dedupe([1, 2, 1]) target output: [1, 2] -*/ - -// Acceptance Criteria: // 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 array", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); +}); + +// Given an array with duplicate strings +// When passed to the dedupe function +// Then it should remove duplicates, preserving first occurrence +test("removes duplicate strings", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); +}); + +// Given an array with duplicate numbers +// When passed to the dedupe function +// Then it should remove duplicates, preserving first occurrence +test("removes duplicate numbers", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); +}); -// Given an array with strings or numbers +// Given an array with mixed duplicates // When passed to the dedupe function -// Then it should remove the duplicate values, preserving the first occurence of each element +// Then it should remove duplicates preserving first occurrence +test("removes duplicates in mixed array", () => { + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..888b49bf6 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,18 @@ +// max.js +// Find the largest numerical element in an array +// Non-numeric values are ignored + function findMax(elements) { + // Filter to keep only numbers (ignores strings, null, undefined, etc.) + const numbersOnly = elements.filter( + (item) => typeof item === "number" && !isNaN(item) + ); + + // If no numbers remain, return -Infinity (consistent with Math.max() behaviour) + if (numbersOnly.length === 0) return -Infinity; + + // Math.max with spread returns the largest number + return Math.max(...numbersOnly); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..1b5ae1ef5 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -1,43 +1,50 @@ -/* Find the maximum element of an array of numbers - -In this kata, you will need to implement a function that find the largest numerical element of an array. - -E.g. max([30, 50, 10, 40]), target output: 50 -E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) - -You should implement this function in max.js, and add tests for it in this file. - -We have set things up already so that this file can see your function from the other file. -*/ - const findMax = require("./max.js"); // Given an empty array // 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([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 +test("returns the largest number from positive and negative values", () => { + expect(findMax([-10, -3, 0, 5, 2])).toBe(5); +}); // 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 to zero when all numbers are negative", () => { + expect(findMax([-5, -1, -3])).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.1, 2.5, 0.9, 2.4])).toBe(2.5); +}); // 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("ignores non-numeric values and returns the max number", () => { + expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); +}); // 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 +// Then it should return -Infinity (same as empty array behaviour) +test("returns -Infinity when array contains only non-numeric values", () => { + expect(findMax(["a", "b", null])).toBe(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..a85fac49d 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,19 @@ +// sum.js +// Sum all numerical elements in an array +// Non-numeric values are ignored + function sum(elements) { + // Filter to keep only numbers (ignores strings, null, undefined, etc.) + const numbersOnly = elements.filter( + (item) => typeof item === "number" && !isNaN(item) + ); + + // If no numbers found, return 0 (consistent with empty array behaviour) + if (numbersOnly.length === 0) return 0; + + // reduce() accumulates all numbers into a single total + // 0 is the initial value of the accumulator + return numbersOnly.reduce((total, num) => total + num, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..f076f1648 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -1,36 +1,43 @@ -/* Sum the numbers in an array - -In this kata, you will need to implement a function that sums the numerical elements of an array - -E.g. sum([10, 20, 30]), target output: 60 -E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements) -*/ - const sum = require("./sum.js"); -// Acceptance Criteria: - // 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 one number, returns that number", () => { + expect(sum([5])).toBe(5); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("correctly sums arrays containing negative numbers", () => { + expect(sum([10, -3, 5])).toBe(12); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("correctly sums decimal numbers", () => { + expect(sum([1.5, 2.5, 3.0])).toBe(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("ignores non-numeric values and sums only numbers", () => { + expect(sum(["hey", 10, "hi", 60, 10])).toBe(80); +}); // 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 +// Then it should return 0 (same as empty array behaviour) +test("returns 0 when array contains only non-numeric values", () => { + expect(sum(["a", null, undefined])).toBe(0); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..ab33fdec9 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,12 +1,17 @@ -// Refactor the implementation of includes to use a for...of loop +// includes.js +// Refactored to use for...of loop instead of a traditional for loop +// Behaviour is identical — only the loop style changed function includes(list, target) { - for (let index = 0; index < list.length; index++) { - const element = list[index]; + // for...of iterates directly over each element value + // No need to manage an index variable manually + for (const element of list) { + // If the current element matches the target, return true immediately if (element === target) { return true; } } + // If no element matched after the full loop, return false return false; } From d3ce55b9f80ba32503e5a500f600e966b7e92f85 Mon Sep 17 00:00:00 2001 From: Mahmoud Shaabo Date: Sat, 21 Mar 2026 12:26:16 +0000 Subject: [PATCH 2/2] fix: address all CJ feedback for sum, dedupe and max tests --- Sprint-1/implement/dedupe.test.js | 16 +++++++++++----- Sprint-1/implement/max.test.js | 5 +++++ Sprint-1/implement/sum.test.js | 4 +++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index cd53f7963..62291b9ae 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -11,7 +11,12 @@ 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, returns a copy of the array", () => { - expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + const original = [1, 2, 3]; + const result = dedupe(original); + expect(result).toEqual([1, 2, 3]); + + // التعديل المطلوب من CJ: التأكد أن النتيجة نسخة جديدة وليست نفس المرجع + expect(result).not.toBe(original); }); // Given an array with duplicate strings @@ -28,9 +33,10 @@ test("removes duplicate numbers", () => { expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); }); -// Given an array with mixed duplicates +// Given an array with mixed types (numbers and strings) // When passed to the dedupe function -// Then it should remove duplicates preserving first occurrence -test("removes duplicates in mixed array", () => { - expect(dedupe([1, 2, 1])).toEqual([1, 2]); +// Then it should treat numbers and strings as different even if they look same +test("removes duplicates in mixed array and distinguishes numbers from strings", () => { + // التعديل المطلوب من CJ: التأكد أن الرقم 1 يختلف عن النص "1" + expect(dedupe([1, "1", 2, "1", 1])).toEqual([1, "1", 2]); }); diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 1b5ae1ef5..a27b41e55 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -40,6 +40,11 @@ test("returns the largest decimal number", () => { // Then it should return the max and ignore non-numeric values test("ignores non-numeric values and returns the max number", () => { expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); + + // CJ's feedback: Ensure numeric strings like "300" are ignored. + // The function should not coerce strings into numbers during comparison. + // In this case, 10 is the maximum because "300" is a string and should be skipped. + expect(findMax(["300", 10, 2])).toBe(10); }); // Given an array with only non-number values diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index f076f1648..e7ea67fe5 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -25,7 +25,9 @@ test("correctly sums arrays containing negative numbers", () => { // When passed to the sum function // Then it should return the correct total sum test("correctly sums decimal numbers", () => { - expect(sum([1.5, 2.5, 3.0])).toBe(7); + // تم التعديل هنا باستخدام toBeCloseTo لضمان دقة حساب الأرقام العشرية + expect(sum([1.5, 2.5, 3.0])).toBeCloseTo(7); + expect(sum([0.1, 0.2])).toBeCloseTo(0.3); }); // Given an array containing non-number values