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

// filter only numbers from array and sort the filtered array. If no numbers return null
const numbers = list
.filter((item) => typeof item === "number")
.sort((a, b) => a - b);
if (numbers.length === 0) return null;

// get numbers array mid point
const middleIndex = Math.floor(numbers.length / 2);
// check if numbers array length is even
const isEven = numbers.length % 2 === 0;
// calculate median based on whether the numbers array length is even or odd
return isEven
? (median = (numbers[middleIndex - 1] + numbers[middleIndex]) / 2)
: numbers[middleIndex];
}

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(array) {
// return empty array if empty input
if (array.length === 0) return [];
return Array.from(new Set(array));
}

module.exports = dedupe;
20 changes: 18 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,29 @@ 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 the original array", () => {
expect(dedupe([4, 5, 6])).toEqual([4, 5, 6]);
});

// 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
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
test("given a number array with duplicates, it returns a new array without duplicates", () => {
expect(dedupe([1, 2, 3, 2, 3])).toEqual([1, 2, 3]);
});

test("given a string array with duplicates, it returns a new array without duplicates", () => {
expect(dedupe(["a", "a", "b", "c"])).toEqual(["a", "b", "c"]);
});

test("given a mixed array with duplicates, it returns a new array without duplicates", () => {
expect(dedupe([1, "a", "a", "b", "c"])).toEqual([1, "a", "b", "c"]);
});
4 changes: 4 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function findMax(elements) {
// filter to return only numbers or an empty array
const numbers = elements.filter((item) => Number.isFinite(item));
// if numbers present in array return largest number or return -Infinity for empty array
return numbers.length ? Math.max(...numbers) : -Infinity;
}

module.exports = findMax;
25 changes: 21 additions & 4 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,45 @@ 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([2])).toBe(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("given an array with positive and negative numbers, returns largest overall number", () => {
expect(findMax([5, -3, 15])).toBe(15);
});
// 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 negative numbers, returns number closest to zero", () => {
expect(findMax([-5, -3, -15])).toBe(-3);
});

// 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 largest decimal number", () => {
expect(findMax([10.5, 2.6, 8.1])).toBe(10.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("given a mixed array with non-number values, returns largest number and ignore non-numbers", () => {
expect(findMax([7, 3, "hello", "#"])).toBe(7);
});
// 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 of non-number values, returns -Infinity", () => {
expect(findMax(["#", "hello", "?"])).toBe(-Infinity);
});
4 changes: 4 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function sum(elements) {
// filter out the numbers and sum them up
return elements
.filter((item) => Number.isFinite(item))
.reduce((acc, num) => acc + num, 0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good use of chaining higher order functions together

}

module.exports = sum;
23 changes: 19 additions & 4 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,39 @@ 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 the number", () => {
expect(sum([1])).toBe(1);
});

// 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 of only negative numbers, returns the sum", () => {
expect(sum([-1, -2])).toBe(-3);
});
// 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 sum", () => {
expect(sum([2, 3.5, 4])).toBe(9.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
// Then it should ignore the non-numerical values and return the sum of the numerical
test("given an array containing non-number values, returns sum of number values", () => {
expect(sum([5, "#", "hello", 2])).toBe(7);
});

// 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 of non-number values, returns 0", () => {
expect(sum(["#", "hello"])).toBe(0);
});
7 changes: 2 additions & 5 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,11 +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];
if (element === target) {
return true;
}
for (const number of list) {
if (number === target) return true;
}
return false;
}
Expand Down
14 changes: 14 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require("fs");
const path = require("path");
const data = fs.readFileSync(path.join(__dirname, "input.txt"), "utf-8");
const total = data
.split("\n")
.map((line) => line.trim())
.filter((line) => line !== "")
.map(Number);

const mapFrequency = (changes) => changes.reduce((acc, num) => acc + num, 0);
console.log(mapFrequency([+1, +1, +1])); // 3
console.log(mapFrequency([+1, +1, -2])); // 0
console.log(mapFrequency([-1, -2, -3])); // -6
console.log(mapFrequency(total)); // 529;
Loading