Skip to content
Open
35 changes: 25 additions & 10 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
// 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) {
// Check if input is a valid array
if (!Array.isArray(list)) {
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 the list to keep only numbers
const numbers = list.filter((item) => typeof item === "number");

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
}
// Return null if no numbers are found
if (numbers.length === 0) {
return null;
}

// Sort the numbers in ascending order
// Note: we use [...numbers] or just numbers because filter already created a new array
numbers.sort((a, b) => a - b);

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

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

// If the count is even, return the average of the two middle numbers
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
}
module.exports = calculateMedian;
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(elements) {
// Create a new empty array to store unique items
let uniqueItems = [];

// Loop through every item in the input array
for (let i = 0; i < elements.length; i++) {
// Check if the item is already in our uniqueItems array
if (!uniqueItems.includes(elements[i])) {
// If not present, add it to the uniqueItems array
uniqueItems.push(elements[i]);
}
}

// Return the final array without duplicates
return uniqueItems;
}

module.exports = dedupe;
36 changes: 13 additions & 23 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
const dedupe = require("./dedupe.js");
/*
Dedupe Array

📖 Dedupe means **deduplicate**
test("given an empty array, it returns an empty array", () => {
// If input is empty, output should be empty
expect(dedupe([])).toEqual([]);
});

In this kata, you will need to deduplicate the elements of an array
test("given an array with no duplicates, it returns a copy of the original array", () => {
// If there are no duplicates, the array stays the same
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
});

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");

// 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
test("given an array with strings or numbers, it removes duplicate values", () => {
// Test if duplicates like 'a' or '1' are removed
expect(dedupe(["a", "a", "b"])).toEqual(["a", "b"]);
expect(dedupe([1, 1, 2, 2, 3])).toEqual([1, 2, 3]);
});
10 changes: 10 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
function findMax(elements) {
// First, we filter the list to keep ONLY numbers
const onlyNumbers = elements.filter((item) => typeof item === "number");

// If there are no numbers at all, we return -Infinity (standard math behavior)
if (onlyNumbers.length === 0) {
return -Infinity;
}

// Use Math.max with the spread operator (...) to find the largest number
return Math.max(...onlyNumbers);
}

module.exports = findMax;
75 changes: 34 additions & 41 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
/* 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");

// Given an array with one number
// When passed to the max function
// Then it should return that number

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values

// 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 empty array, returns -Infinity", () => {
// Test case for empty input
expect(findMax([])).toBe(-Infinity);
});

test("given an array with one number, it returns that number", () => {
// Test case for single element
expect(findMax([5])).toBe(5);
});

test("given an array with both positive and negative numbers, it returns the largest", () => {
// Test case for mixed positive and negative numbers
expect(findMax([-1, 10, -5])).toBe(10);
});

test("given an array with just negative numbers, it returns the closest one to zero", () => {
// Test case for negative numbers
expect(findMax([-10, -1, -5])).toBe(-1);
});

test("given an array with decimal numbers, it returns the largest decimal number", () => {
// Test case for decimal numbers
expect(findMax([1.1, 1.5, 1.2])).toBe(1.5);
});

test("given an array with non-number values, it returns the max and ignores others", () => {
// Test case to ensure non-numeric values are ignored
expect(findMax([10, "hi", 20, null])).toBe(20);
});

test("given an array with only non-number values, it returns -Infinity", () => {
// Test case where no valid numbers exist in the array
expect(findMax(["a", "b", "c"])).toBe(-Infinity);
});
14 changes: 14 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
function sum(elements) {
// Start with a total of 0
let total = 0;

// Go through every item in the array one by one
for (let i = 0; i < elements.length; i++) {
// We only care about numbers, so we check the type
if (typeof elements[i] === "number") {
// Add the number to our total
total = total + elements[i];
}
}

// Return the final result
return total;
}

module.exports = sum;
48 changes: 19 additions & 29 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
/* 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([10])).toBe(10);
});

// 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 correct total", () => {
expect(sum([10, -5, 5])).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 correct total", () => {
expect(sum([1.5, 2.5, 1])).toBe(5);
});

// 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 them", () => {
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
test("given an array with only non-number values, returns 0", () => {
// 0 is the 'least surprising value' since it's the starting point
expect(sum(["apple", "orange"])).toBe(0);
});
7 changes: 3 additions & 4 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

// This function checks if the target exists in the list
function includes(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
// We use for...of for a cleaner syntax (refactoring)
for (const element of list) {
if (element === target) {
return true;
}
Expand Down
2 changes: 0 additions & 2 deletions Sprint-1/refactor/includes.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Refactored version of includes should still pass the tests below:

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

test("returns true when target is in array", () => {
Expand Down