Skip to content
Open
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
29 changes: 26 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,32 @@
// 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;
// Check if input is an array
if (!Array.isArray(list)) {
return null;
}

// Filter out non-numbers (keep only numbers)
const numbers = list.filter(item => typeof item === 'number' && !isNaN(item));

// If no numbers in the array, return null
if (numbers.length === 0) {
return null;
}

// Sort the numbers in ascending order
const sorted = [...numbers].sort((a, b) => a - b);
Comment on lines +22 to +23
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to make a copy of numbers before sorting?


const middleIndex = Math.floor(sorted.length / 2);

// If length is odd, return the middle element
if (sorted.length % 2 !== 0) {
return sorted[middleIndex];
}
// If length is even, return the average of the two middle elements
else {
return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
}
}

module.exports = calculateMedian;
8 changes: 7 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
function dedupe() {}
function dedupe(elements) {
// Using a Set to remove duplicates while preserving insertion order
// Then spread the Set back into an array
return [...new Set(elements)];
}

module.exports = dedupe;
44 changes: 41 additions & 3 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,51 @@ 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 arr1 = [1, 2, 3, 4, 5];
expect(dedupe(arr1)).toEqual([1, 2, 3, 4, 5]);
expect(dedupe(arr1)).not.toBe(arr1); // Should be a copy, not the same reference

const arr2 = ['a', 'b', 'c', 'd'];
expect(dedupe(arr2)).toEqual(['a', 'b', 'c', 'd']);

const arr3 = [10, 20, 30];
expect(dedupe(arr3)).toEqual([10, 20, 30]);
});

// 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 occurrence of each element
test("given an array with strings or numbers, it removes duplicate values, preserving the first occurrence of each element", () => {
// String duplicates
expect(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']);

// Number duplicates
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);

// Mixed duplicates
expect(dedupe([1, 2, 1])).toEqual([1, 2]);

// Multiple duplicate occurrences
expect(dedupe([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])).toEqual([1, 2, 3, 4]);

// Different data types (they're distinct, so no duplicates)
expect(dedupe([1, '1', 1, '1'])).toEqual([1, '1']);

// Mixed types with duplicates within same type
expect(dedupe(['x', 'y', 'x', 'z', 'y', 'x'])).toEqual(['x', 'y', 'z']);

// Numbers in different order
expect(dedupe([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])).toEqual([3, 1, 4, 5, 9, 2, 6]);

// Single element array
expect(dedupe([42])).toEqual([42]);
expect(dedupe(['hello'])).toEqual(['hello']);
});
11 changes: 11 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@

function findMax(elements) {
// Filter out non-numeric values
const numbers = elements.filter(element => typeof element === 'number');

Comment on lines +3 to +5
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you expect from the following function calls (on extreme cases)?
Does your function return the value you expected?

findMax([NaN])
findMax([0, NaN, 1])

// If no numbers in array, return -Infinity
if (numbers.length === 0) {
return -Infinity;
}

// Find and return the maximum value
return Math.max(...numbers);
}

module.exports = findMax;
47 changes: 33 additions & 14 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,62 @@
/* 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);
expect(findMax([-10])).toBe(-10);
expect(findMax([0])).toBe(0);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test("given an array with both positive and negative numbers, returns the largest number overall", () => {
expect(findMax([30, 50, 10, 40])).toBe(50);
expect(findMax([-5, 10, -20, 30])).toBe(30);
expect(findMax([-100, -200, 0, 150])).toBe(150);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("given an array with just negative numbers, returns the closest one to zero", () => {
expect(findMax([-5, -10, -3, -8])).toBe(-3);
expect(findMax([-1, -100, -50])).toBe(-1);
expect(findMax([-0.5, -0.8, -0.1])).toBe(-0.1);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("given an array with decimal numbers, returns the largest decimal number", () => {
expect(findMax([1.5, 2.7, 1.2, 3.8])).toBe(3.8);
expect(findMax([-1.5, -2.7, -1.2, -0.5])).toBe(-0.5);
expect(findMax([0.1, 0.01, 0.001])).toBe(0.1);
});

// 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("given an array with non-number values, returns the max and ignores non-numeric values", () => {
expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60);
expect(findMax([20, 'abc', 30, 'def', 40])).toBe(40);
expect(findMax(['x', 'y', 'z', 100, 50])).toBe(100);
});
Comment on lines +49 to +53
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a string representing a valid numeric literal (for example, "300") is compared to a number,
JavaScript first converts the string into its numeric equivalent before performing the comparison.
As a result, the expression 20 < "300" evaluates to true.

To test if the function can correctly ignore non-numeric values,
consider including a string such as "300" in the relevant test cases.


// 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, returns -Infinity", () => {
expect(findMax(['hey', 'hi', 'hello'])).toBe(-Infinity);
expect(findMax([true, false, null])).toBe(-Infinity);
expect(findMax([{}, [], 'string'])).toBe(-Infinity);
});
5 changes: 5 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
function sum(elements) {
// Filter out non-numeric values
const numbers = elements.filter(element => typeof element === 'number');

// Sum all the numbers using reduce
return numbers.reduce((total, current) => total + current, 0);
}

module.exports = sum;
45 changes: 34 additions & 11 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
/* 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 just one number, returns that number", () => {
expect(sum([42])).toBe(42);
expect(sum([-10])).toBe(-10);
expect(sum([0])).toBe(0);
expect(sum([3.14])).toBe(3.14);
});

// 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 containing negative numbers, returns the correct total sum", () => {
expect(sum([10, -5, 20, -3])).toBe(22);
expect(sum([-10, -20, -30])).toBe(-60);
expect(sum([5, -5, 10, -10])).toBe(0);
expect(sum([-1, -2, -3, -4])).toBe(-10);
});

// 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 the correct total sum", () => {
expect(sum([1.5, 2.5, 3.0])).toBe(7);
expect(sum([0.1, 0.2, 0.3])).toBeCloseTo(0.6);
expect(sum([1.99, 2.99, 3.99])).toBeCloseTo(8.97);
expect(sum([-1.5, 2.5, -1.0])).toBe(0);
});

// 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 containing non-number values, ignores non-numerical values and returns sum of numerical elements", () => {
expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80);
expect(sum([20, 'abc', 30, 'def', 40])).toBe(90);
expect(sum(['x', 'y', 'z', 100, 50])).toBe(150);
expect(sum([true, 5, false, 10, null, 15])).toBe(30);
expect(sum([{}, [], 25, 'string', 35])).toBe(60);
});

// 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("given an array with only non-number values, returns 0", () => {
expect(sum(['hey', 'hi', 'hello'])).toBe(0);
expect(sum([true, false, null])).toBe(0);
expect(sum([{}, [], 'string', undefined])).toBe(0);
expect(sum(['a', 'b', 'c'])).toBe(0);
});
Loading
Loading