Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
27 changes: 18 additions & 9 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// Fix this implementation
// Start by running the tests for this function
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory
function calculateMedian(list) {
if (!Array.isArray(list) || list.length === 0) return null;

// 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).
// Filter out non-number values
const numbers = list.filter(item => typeof item === "number");
if (numbers.length === 0) return null;

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
// Sort numbers in ascending order
numbers.sort((a, b) => a - b);

Choose a reason for hiding this comment

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

Do you need to include this anonymous sorting function as an argument to sort?

Copy link
Author

Choose a reason for hiding this comment

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

Hello LonMcGregor,

Thank you for the feedback!

I double-checked the sort function—using (a, b) => a - b is necessary to sort numbers numerically; otherwise, JavaScript sorts them as strings, which would break the median calculation.
I also removed the splice approach from my earlier attempt to avoid mutating the array—returning numbers**[middleIndex]** directly works cleanly for odd-length arrays.

Choose a reason for hiding this comment

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

Good answer!


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

// If odd number of elements, return middle
if (numbers.length % 2 !== 0) {
return numbers[middleIndex];
}

// If even, return average of two middle numbers
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
}

module.exports = calculateMedian;

8 changes: 8 additions & 0 deletions Sprint-1/fix/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"devDependencies": {
"jest": "^30.1.3"
},
"scripts": {
"test": "jest"
}
}
19 changes: 18 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
function dedupe() {}
function dedupe(arr) {
if (!Array.isArray(arr)) return [];

const seen = new Set();
const result = [];

for (const item of arr) {
if (!seen.has(item)) {
seen.add(item);
result.push(item);
}
}

return result;
}

module.exports = dedupe;

44 changes: 24 additions & 20 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
const dedupe = require("./dedupe.js");
/*
Dedupe Array
describe("dedupe function", () => {

📖 Dedupe means **deduplicate**
// Test 1: empty array
test("given an empty array, it returns an empty array", () => {
expect(dedupe([])).toEqual([]);
});

In this kata, you will need to deduplicate the elements of an array
// Test 2: array with no duplicates
test("given an array with no duplicates, it returns a copy of the original array", () => {
const arr = [1, 2, 3, 4];
expect(dedupe(arr)).toEqual(arr);
expect(dedupe(arr)).not.toBe(arr); // ensure it returns a new array, not the same reference
});

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]
*/
// Test 3: array with duplicates (strings)
test("removes duplicate strings, preserving first occurrence", () => {
expect(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']);
});

// Acceptance Criteria:
// Test 4: array with duplicates (numbers)
test("removes duplicate numbers, preserving first occurrence", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});

// 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 5: mixed array
test("removes duplicates in mixed arrays", () => {
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array

// 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
});
14 changes: 13 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
function findMax(elements) {
function findMax(arr) {
if (!Array.isArray(arr) || arr.length === 0) return -Infinity;

let max = -Infinity;

for (const item of arr) {
if (typeof item === "number" && item > max) {
max = item;
}
}

return max;
}

module.exports = findMax;

65 changes: 31 additions & 34 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
/* 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.
const findMax = require("./max.js");

We have set things up already so that this file can see your function from the other file.
*/
describe("findMax function", () => {

const findMax = require("./max.js");
// Test 1: empty array
test("given an empty array, returns -Infinity", () => {
expect(findMax([])).toBe(-Infinity);
});

// 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 2: array with one number
test("given an array with one number, returns that number", () => {
expect(findMax([42])).toBe(42);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
// Test 3: array with positive and negative numbers
test("returns the largest number from mixed positive and negative numbers", () => {
expect(findMax([3, -2, 7, -10, 5])).toBe(7);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
// Test 4: array with only negative numbers
test("returns the closest number to zero for negative numbers", () => {
expect(findMax([-10, -2, -30])).toBe(-2);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
// Test 5: array with decimal numbers
test("returns the largest decimal number", () => {
expect(findMax([1.1, 2.5, 2.49])).toBe(2.5);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
// Test 6: array with non-number values
test("ignores non-numeric values and returns the largest number", () => {
expect(findMax([10, "apple", 20, null, 5])).toBe(20);
});

// 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 7: array with only non-number values
test("array with only non-number values returns -Infinity", () => {
expect(findMax(["a", null, "b"])).toBe(-Infinity);
});

// 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
});
8 changes: 8 additions & 0 deletions Sprint-1/implement/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"devDependencies": {
"jest": "^30.1.3"
},
"scripts": {
"test": "jest"
}
}
8 changes: 7 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function sum(elements) {
function sum(arr) {
if (!Array.isArray(arr)) return 0;

return arr.reduce((total, item) => {
return typeof item === "number" ? total + item : total;
}, 0);
}

module.exports = sum;

55 changes: 27 additions & 28 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
/* 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:
describe("sum function", () => {

// Test 1: empty array
test("given an empty array, returns 0", () => {
expect(sum([])).toBe(0);
});

// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")
// Test 2: array with one number
test("given an array with one number, returns that number", () => {
expect(sum([42])).toBe(42);
});

// Given an array with just one number
// When passed to the sum function
// Then it should return that number
// Test 3: array with positive and negative numbers
test("returns the correct total sum for positive and negative numbers", () => {
expect(sum([10, -5, 15, -3])).toBe(17);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
// Test 4: array with decimal numbers
test("returns the correct total sum for decimal/float numbers", () => {
expect(sum([1.5, 2.3, 3.2])).toBeCloseTo(7.0); // Use toBeCloseTo for floating points
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
// Test 5: array with non-number values
test("ignores non-numeric values and sums numerical elements", () => {
expect(sum([10, "a", 20, null, 5])).toBe(35);
});

// 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 6: array with only non-number values
test("returns 0 if there are no numeric elements", () => {
expect(sum(["a", null, "b"])).toBe(0);
});

// 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
});
7 changes: 3 additions & 4 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Refactor the implementation of includes to use a for...of loop

// Check if a list includes a target value
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 All @@ -11,3 +9,4 @@ function includes(list, target) {
}

module.exports = includes;

8 changes: 8 additions & 0 deletions Sprint-1/refactor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"devDependencies": {
"jest": "^30.1.3"
},
"scripts": {
"test": "jest"
}
}
Loading