Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -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;
47 changes: 31 additions & 16 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
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", () => {
const original = [1, 2, 3];
const result = dedupe(original);
expect(result).toEqual([1, 2, 3]);

// التعديل المطلوب من CJ: التأكد أن النتيجة نسخة جديدة وليست نفس المرجع
expect(result).not.toBe(original);
});

Comment thread
cjyuan marked this conversation as resolved.
// 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 types (numbers and strings)
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurence of each element
// 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]);
});
14 changes: 14 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -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;
42 changes: 27 additions & 15 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
/* 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);

// 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);
});
Comment thread
cjyuan marked this conversation as resolved.

// 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);
});
15 changes: 15 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -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;
33 changes: 21 additions & 12 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
/* 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", () => {
// تم التعديل هنا باستخدام toBeCloseTo لضمان دقة حساب الأرقام العشرية
expect(sum([1.5, 2.5, 3.0])).toBeCloseTo(7);
expect(sum([0.1, 0.2])).toBeCloseTo(0.3);
});
Comment thread
cjyuan marked this conversation as resolved.

// 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);
});
11 changes: 8 additions & 3 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
Loading