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
25 changes: 22 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,28 @@
// 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;

// first of all, checking if 'list' is an array
if (Array.isArray(list)) {

const filteredArr = list.filter(x => typeof x === 'number');

if (filteredArr.length < 2) {
return null;
}

// using 'spread operator' copies the array without mutating it
const sortedList = [...filteredArr].sort((a, b) => a - b);

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

if (sortedList.length % 2 === 0) {
return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
} else {
return sortedList[middleIndex]
}
}
return null;
}

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(array) {
let arr = array.filter((item, index) => {

// indexOf() returns the first index where that value appears in the array.
if (array.indexOf(item) === index) {
return item;
}
})

return arr;
}

module.exports = dedupe;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@ 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("return an empty array for 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("if no duplicates, return the copy of the original array", () => {
expect(dedupe([1, 3, 5, 7])).toEqual([1, 3, 5, 7]);
})

// 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("an array with strings strings or numbers", () => {
expect(dedupe([2, 2, 3, 3, 3, "Black", "Black", "Black", "Green"])).toEqual([2, 3, "Black", "Green"]);
})
8 changes: 8 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function findMax(elements) {
const filteredArr = elements.filter(x => typeof x === 'number');

if (filteredArr.length == 0) {
return -Infinity;
}

// using 'spread sytax'
return Math.max(...filteredArr);
}

module.exports = findMax;
32 changes: 31 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,58 @@ 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("returns -Infinity for an empty array", () => {
expect(findMax([])).toEqual(-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, should return that number", () => {
expect(findMax([8])).toEqual(8);
expect(findMax([-5])).toEqual(-5);
expect(findMax([0])).toEqual(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("return the largest number overall", () => {
expect(findMax([-8, -4, 0, 4, 8])).toEqual(8);
expect(findMax([-3, -2, -1, 4, 2, 3])).toEqual(4);
})

// 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, should return closest to 0", () => {
expect(findMax([-2, -4, -1, -3, -100])).toEqual(-1);
})

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

test("an array with decimal numbers, should return the largest decimal number", () => {
expect(findMax([0.1, 0.2, 0.9, 0.8, 0.3, 0.7, 0.4, 0.6])).toEqual(0.9);
})

// 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("ignore the non-numeric values", () => {
expect(findMax(["Blue", 3, "White", "Orange", "Pink"])).toEqual(3);
})

// 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("an array with only non-number values", () => {
expect(findMax(["Blue", "White", "Orange", "Pink"])).toEqual(-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) {

// const filteredArr = elements.filter(x => typeof x === 'number');
// return filteredArr.reduce((a, b) => a + b, 0);

return elements.filter(x => typeof x === 'number').reduce((a, b) => a + b, 0);
}

module.exports = sum;
27 changes: 26 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,49 @@ 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("return 0 for an empty array", () => {
expect(sum([])).toEqual(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, return that number", () => {
expect(sum([3])).toEqual(3);
expect(sum([-3])).toEqual(-3);
expect(sum([0])).toEqual(0);
})

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

test("return the correct total when passed negative numbers", () => {
expect(sum([-3, -6, -1])).toEqual(-10);
})

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("decimal number arrays", () => {
expect(sum([0.2, 0.4, 0.2])).toEqual(0.8);
})

// 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("an array with non-numbers", () => {
expect(sum([2, "Blue", 3, "Black", "Green"])).toEqual(5);
})

// 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("an array with non-number values only", () => {
expect(sum([true, false, "Black"])).toEqual(0);
expect(sum([undefined, null, "Black"])).toEqual(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) {
if (element === target) {
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function sum(list) {
return list.reduce((a, b) => a + b, 0);
}