Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
90372b6
feat: add a function to pass the array of number, positives and negat…
Grajales-K Feb 27, 2026
9575183
feat: implement immutable sorting using spread operator for median ca…
Grajales-K Feb 27, 2026
7f1e727
feat: add validation for empty or non-array input in median calculation
Grajales-K Feb 28, 2026
9e447e7
feat: enhance median calculation by filtering non-numeric values and …
Grajales-K Feb 28, 2026
bb34f14
feat: add data validation and non-numeric filtering to median calcula…
Grajales-K Feb 28, 2026
0db6d2e
refactor: cleaning the function and leave only the reusable code
Grajales-K Feb 28, 2026
7591135
test: add unit tests for dedupe function with various input scenarios
Grajales-K Mar 12, 2026
efb99b9
feat: implement deduplication function to remove duplicate values fro…
Grajales-K Mar 12, 2026
5da81e2
refactor: remove unnecessary check for empty array in dedupe function
Grajales-K Mar 12, 2026
799078c
refactor: simplify dedupe function by removing unnecessary conditiona…
Grajales-K Mar 12, 2026
169e5b7
test: add unit tests for max function covering various input scenarios
Grajales-K Mar 16, 2026
38450fd
feat: add validation for empty input and filter non-numeric values in…
Grajales-K Mar 16, 2026
8875c3e
refactor: simplify findMax function by filtering non-numeric values
Grajales-K Mar 16, 2026
f45cbf4
test: add unit tests for sum function covering various input scenarios
Grajales-K Mar 17, 2026
88e8c1b
test: fix expected assertions in unit tests for sum function
Grajales-K Mar 17, 2026
b490a39
feat: implement sum function with filtering for non-numeric values
Grajales-K Mar 17, 2026
9e3e2a9
refactor: simplify sum function by combining filtering and sum the nu…
Grajales-K Mar 17, 2026
e33e09c
refactor: improve test readability and consistency in includes tests
Grajales-K Mar 17, 2026
4718ab3
refactor: simplify includes function by using for...of loop
Grajales-K Mar 17, 2026
2b09a9b
Merge branch 'main' into Sprint-1
Grajales-K Mar 17, 2026
57e4e97
refactor: optimize median calculation by removing unnecessary array s…
Grajales-K Mar 20, 2026
b3838b5
refactor: clean up median function by improving number filtering logic
Grajales-K Mar 20, 2026
19331eb
fix: add new name parameter for better readability
Grajales-K Mar 20, 2026
aa28437
refactor: enhance dedupe test for clarity and ensure input immutability
Grajales-K Mar 20, 2026
788a622
refactor: improve number filtering logic in findMax function to exclu…
Grajales-K Mar 20, 2026
71807f7
test: add cases to validate findMax function handling of NaN values
Grajales-K Mar 20, 2026
c317ce7
fix: use prettier for indentation
Grajales-K Mar 20, 2026
5eb205b
test: add cases to validate sum function handling of NaN and Infinity…
Grajales-K Mar 20, 2026
31d489d
refactor: improve sum function to exclude NaN values during calculation
Grajales-K Mar 20, 2026
1d9349d
fix: use toBeCloseTo for floating point precision and handle edge cases
Grajales-K Mar 23, 2026
8116813
test: add edge cases for sum function to handle floating point precision
Grajales-K Mar 23, 2026
d529f18
fix: update input data for aoc-2018-day1 to correct values
Grajales-K Mar 23, 2026
c55acc1
fix: dedupe test for clarity and ensure input immutability
Grajales-K Mar 23, 2026
4e2a405
fix: test ignore no numeric values
Grajales-K Mar 23, 2026
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
36 changes: 33 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,40 @@
// 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).

// [...list] create a new exactly array but independent
// const sortArr = list.sort((a, b) => a - b); this modify the array

// order the array to find the middle number
//using [...list] is a safe immutable sorting
// const sortArr = [...list].sort((a, b) => a - b);

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
// if is not an array
if (!Array.isArray(list)) {
return null;
}

// Filter for valid numbers only typeof n === "number"' already excludes null, strings, and objects
const numsOnly = list.filter((n) => typeof n === "number" && !isNaN(n));

// if return numbers check if after cleaning the length is same as expected or empty
if (numsOnly.length === 0) {
return null;
}

// now we change to use the numsOnly otherwise will take string as numbers
const sortArr = numsOnly.sort((a, b) => a - b);

// divide the array to find the middle position.
const middleIndex = Math.floor(sortArr.length / 2);

//if residual is 0, when the array is even
if (sortArr.length % 2 === 0) {
const leftHalf = sortArr[middleIndex - 1];
const rightHalf = sortArr[middleIndex];
return (leftHalf + rightHalf) / 2;
}
return sortArr[middleIndex];
}

module.exports = calculateMedian;
11 changes: 10 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
function dedupe() {}
/**
* Deduplicates an array of elements (strings, numbers, etc.)
* @param {Array} items - The array containing potential duplicates
*/

function dedupe(items) {
return [...new Set(items)];
}

module.exports = dedupe;
31 changes: 27 additions & 4 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const dedupe = require("./dedupe.js");
/*
Dedupe Array

📖 Dedupe means **deduplicate**
📖 Dedupe means **deduplicate**

In this kata, you will need to deduplicate the elements of an array

Expand All @@ -16,13 +16,36 @@ 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 returns a copy of the original array", () => {
const input = ["a", "b", "c"];
const result = dedupe(input);

expect(result).toEqual(["a", "b", "c"]); // Check that the result is correct
expect(input).toEqual(["a", "b", "c"]); // Check that the original input array is unchanged
expect(result).not.toBe(input); // Check that the result has created a new array (not the same reference as input)
});
Comment thread
Grajales-K marked this conversation as resolved.
Comment thread
Grajales-K marked this conversation as resolved.

// 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.
// Then it should remove the duplicate values, preserving the first occurence of each element

describe("Describes dedupe() with duplicate values", () => {
[
{ input: [1, 2, 2, 3], expected: [1, 2, 3] },
{ input: [1, 1, 3, 4, 4, 4, 5], expected: [1, 3, 4, 5] },
{ input: ["a", "b", "a", "c"], expected: ["a", "b", "c"] },
{ input: [1, "1", 1], expected: [1, "1"] },
{ input: [5, 1, 5, 5, 2], expected: [5, 1, 2] },
].forEach(({ input, expected }) => {
it(`should return [${expected}] for input [${input}]`, () => {
expect(dedupe(input)).toEqual(expected);
});
});
});
7 changes: 7 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
// 1. filter: leep only elements that are onlyNumbers.
// 2. spread (...) take the numbers out of the Array
// 3. Math.max find the largest number and return -infinity if empty.

function findMax(elements) {
const onlyNumbers = elements.filter((element) => typeof element === "number" && !Number.isNaN(element))
return Math.max(...onlyNumbers);
}
Comment thread
Grajales-K marked this conversation as resolved.

module.exports = findMax;

52 changes: 48 additions & 4 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,83 @@ In this kata, you will need to implement a function that find the largest numeri
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.
You should implement this function in max.js, and add tests for it in this file.p

We have set things up already so that this file can see your function from the other file.

Driven Development (BDD), format Given / When / Then.
*/

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("should return -Infinity when given an empty array", () => {
expect(findMax([])).toEqual(-Infinity);
});
// Given an array with one number
// When passed to the max function
// Then it should return that number
test("should return the number itself when the array has only one element", () => {
expect(findMax([2])).toEqual(2);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test("should return the largest number when given both positive and negative numbers", () => {
expect(findMax([-1, -3, 0, 3])).toEqual(3);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("should return the closest number to zero when given only negative numbers", () => {
expect(findMax([-1, -3, -2, 0])).toEqual(0);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("should return the largest decimal number correctly", () => {
expect(findMax([1.5, 2.5, 0.5])).toEqual(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("should ignore non-numeric values (strings, etc.) and return the max", () => {
expect(findMax(["hey", 10, "hi", 60, 10])).toEqual(60);
});

// given an array with non-numeric values
// When passed to the max function
// Then it should strictly ignore non-numeric values, including numeric strings like '300'
test("should strictly ignore non-numeric values, including numeric strings like '300'", () => {
const input = ["hey", 10, "300", 60, 10];
const result = findMax(input);

expect(result).toEqual(60); // Check that the result is correct
});

// Given an array with numbers and NaN
// When passed to the findMax function
// Then it should ignore NaN and return the largest number
test("should ignore NaN and return the max of the remaining numbers", () => {
expect(findMax([0, NaN, 1])).toEqual(1);
});

// 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 with only non-number values, it returns -Infinity", () => {
expect(findMax(["abc", "def"])).toEqual(-Infinity);
});

Comment thread
Grajales-K marked this conversation as resolved.
// Given an array with only NaN values
// When passed to the findMax function
// Then it should return -Infinity
test("should return -Infinity when the array contains only NaN", () => {
expect(findMax([NaN])).toEqual(-Infinity);
});
6 changes: 6 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function sum(elements) {
return elements.reduce(
(acc, curr) =>
typeof curr === "number" && !Number.isNaN(curr) ? acc + curr : acc,
0
);
}
Comment thread
Grajales-K marked this conversation as resolved.

module.exports = sum;

48 changes: 47 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,70 @@ 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("Should return 0 for empty array", () => {
expect(sum([])).toEqual(0);
});

// Given an array with just one number
// When passed to the sum function
// Then it should return that number
test("Should return the number itself for an array with one number", () => {
expect(sum([5])).toEqual(5);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("Should return the correct sum even when the array contains negative numbers", () => {
expect(sum([-1, -2, -3])).toEqual(-6);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("Should return the correct sum even when the array contains decimal numbers", () => {
expect(sum([1.4, 1.6, 5.8])).toEqual(8.8);
});
Comment thread
Grajales-K marked this conversation as resolved.

describe("sum() edge cases and floating point precision", () => {
[
{ input: [0.1, 0.2], expected: 0.3 },
{ input: [0.7, 0.2], expected: 0.9 },
{ input: [1.2, 0.6, 0.005], expected: 1.805 },
{ input: [0.005, 0.6, 1.2], expected: 1.805 },
].forEach(({ input, expected }) => {
it(`should return ${expected} for input [${input.join(", ")}]`, () => {
expect(sum(input)).toBeCloseTo(expected);
});
});
});


// 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("should return the sum of the numerical elements and ignore non-numerical values", () => {
expect(sum(['hey', 10, 'hi', 60, 10])).toEqual(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
test("Should return 0 for an array with only non-numbers", () => {
expect(sum(['hey', 'hi', 'hello'])).toEqual(0);
})

// Given an array with NaN and numbers
// When passed to the sum function
// Then it should ignore NaN and return the sum of the numbers
test("should ignore NaN and return the correct sum", () => {
expect(sum([NaN, 1, 5])).toEqual(6);
});


// Given an array with Infinity and -Infinity
// When passed to the sum function
// Then it should return NaN (standard JS behavior for infinite subtraction)
test("should return NaN when summing Infinity and -Infinity", () => {
expect(sum([Infinity, -Infinity])).toBeNaN();
});
3 changes: 1 addition & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
33 changes: 9 additions & 24 deletions Sprint-1/refactor/includes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,22 @@

const includes = require("./includes.js");

test("returns true when target is in array", () => {
const currentOutput = includes(["a", "b", "c", "d"], "c");
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);
});
test("Return true if the array contains the elements desired", () => {
expect(includes(["a", "b", "c", "d"], "c")).toBe(true);
})

test("returns false when target not in array", () => {
const currentOutput = includes([1, 2, 3, 4], "a");
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
expect(includes([1, 2, 3, 4], "a")).toBe(false);
});

test("returns true when the target is in array multiple times", () => {
const currentOutput = includes([1, 2, 2, 3], 2);
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);
expect(includes([1, 2, 2, 3], 2)).toBe(true);
});

test("returns false for empty array", () => {
const currentOutput = includes([]);
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
expect(includes([], "a")).toBe(false);
});

test("searches for null", () => {
const currentOutput = includes(["b", "z", null, "a"], null);
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);
});
test("should find null when it exists in the array", () => {
expect(includes(["b", "z", null, "a"], null)).toBe(true);
});
Loading
Loading