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
41 changes: 38 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,45 @@
// 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).

// function calculateMedian(list) {
// const middleIndex = Math.floor(list.length / 2); // Finds the middle index of the array (rounds down if even)
// const median = list.splice(middleIndex, 1)[0]; // Removes the value at the middle index from the array and gets that value
// return median; // Returns the value found above as the median
// }

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

let numbers = [];
for (let i = 0; i < list.length; i++) {
if (typeof list[i] === "number") {
// Checks if the item in the array is a number, if so, push it to the numbers array.
numbers.push(list[i]);
}
}
if (numbers.length === 0) {
// This part checks if the numbers array we created is empty. If so, we return null.
return null;
}

let sorted = numbers.slice();
sorted.sort(function (a, b) {
// sorted is the sorted version of the numbers array. We created a new array so we don't lose the original.
return a - b;
});

let numbersAmount = sorted.length;
if (numbersAmount % 2 === 1) {
// Checks if the numbers in the array is odd. For example 3 numbers in the array.
return sorted[Math.floor(n / 2)]; // Returns the middle number of the array. We count from 0 so for example 0, 1, "2", 3, 4.
} else {
let mid1 = sorted[n / 2 - 1];
let mid2 = sorted[n / 2];
return (mid1 + mid2) / 2; // We take the two middle numbers avarege with divide by 2 to get the median.
}
}

module.exports = calculateMedian;
14 changes: 13 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
function dedupe() {}
function dedupe(list) {
let result = [];

for (let i = 0; i < list.length; i++) {
if (!result.includes(list[i])) {
result.push(list[i]);
}
}

return result;
}

module.exports = dedupe;
22 changes: 21 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,32 @@ E.g. dedupe([1, 2, 1]) target output: [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", () => {
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
});

// 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

// Duplicated strings.
test("given an array of duplicate strings, it removes duplicates and keeps the first occurrence", () => {
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
});

// Duplicated numbers.
test("given an array of duplicate numbers, it removes duplicates and keeps the first occurrence", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});

// Mixed vanlues of numbers and strings.
test("given a mixed array with duplicates, it preserves the first occurrence of each element", () => {
expect(dedupe([1, "a", 1, "a", 2, "b", 2])).toEqual([1, "a", 2, "b"]);
});
13 changes: 12 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
function findMax(elements) {
function findMax(arrayElements) {
let max = -Infinity; // define max first, if the array is empty, return -Infinity
for (i = 0; i < arrayElements.length; i++) {
if (typeof arrayElements[i] === "number") {
// first checks if the array element is a number before moving on.
if (arrayElements[i] > max) {
// Finds the largest number after the === numbers test.
max = arrayElements[i]; // redefine max with the maximum number in the array.
}
}
}
return max;
}

module.exports = findMax;
37 changes: 34 additions & 3 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* 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.
In this kata, you will need to implement a function that finds 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)
E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (max ignores any non-numerical elements)

You should implement this function in max.js, and add tests for it in this file.

Expand All @@ -16,28 +16,59 @@ const findMax = require("./max.js");
// 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([44])).toBe(44);
});

// 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([-10, 0, 5, 30, -40])).toBe(30);
});

// 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 only negative numbers, returns the number closest to zero", () => {
expect(findMax([-20, -4, -43, -33, -7])).toBe(-4);
});

// 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([2.3, 4.4, 5.5, 1.2, 6.6])).toBe(6.6);
});

// 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 number and ignores non-numeric values", () => {
expect(findMax(["Spongebob", 2, "Squarepants", 5, 8, "patrick", 20])).toBe(
20
);
});

// 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-numeric values, returns -Infinity", () => {
expect(findMax(["Spongebob", "Squarepants", null, undefined])).toBe(
-Infinity
);
});
9 changes: 9 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function sum(elements) {
let total = 0;

for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number") {
total = total + elements[i];
}
}

return total;
}

module.exports = sum;
25 changes: 24 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,47 @@ 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("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([44])).toBe(44);
});

// 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, -3, 8])).toBe(0);
});

// 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 numbers, returns the correct total sum", () => {
expect(sum([1.5, 2.5, 3.5])).toBe(7.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 and returns the sum of 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

test("given an array with only non-number values, returns 0", () => {
expect(sum(["hello", null, undefined, "banana"])).toBe(0);
});
4 changes: 2 additions & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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) {
// const element = list[index]; => We don't need this line of code when using the for...of loop.
if (element === target) {
return true;
}
Expand Down