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
17 changes: 13 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
// 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;
if (!Array.isArray(list)) return null;

const numericValue = list.filter(n => typeof n === 'number' && !isNaN(n));
if (numericValue.length === 0) return null;

const sorted = [...numericValue].sort((a, b) => a - b);

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

return sorted.length % 2 === 0
? (sorted[middleIndex - 1] + sorted[middleIndex]) / 2
: sorted[middleIndex];
}

module.exports = calculateMedian;
module.exports = calculateMedian;
3 changes: 1 addition & 2 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const calculateMedian = require("./median.js");

describe("calculateMedian", () => {

[
{ input: [1, 2, 3], expected: 2 },
{ input: [1, 2, 3, 4, 5], expected: 3 },
Expand Down Expand Up @@ -47,4 +47,3 @@ describe("calculateMedian", () => {
].forEach(({ input, expected }) =>
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
});
7 changes: 6 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
function dedupe() {}
function dedupe(groupNumber) {
return [...new Set(groupNumber)];

}

module.exports = dedupe
23 changes: 22 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,33 @@ 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", () => {
const input = [];
const result = dedupe(input);
expect(result).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", () => {
const input = [1, 2, 3, 4, 5];
const result = dedupe(input);
expect(result).toEqual([1, 2, 3, 4, 5]);
});

// 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 a strings", () => {
const input = ["a", "a", "b", "b", "c", "c", "d", "d", "e", "e"];
const result = dedupe(input);
expect(result).toEqual(["a", "b", "c", "d", "e"]);
});

test("given an array with number", () => {
const input = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5];
const result = dedupe(input);
expect(result).toEqual([1, 2, 3, 4, 5]);
});
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) {
if (!Array.isArray(elements)) return null
const maxNum = elements.filter(n => typeof n === 'number' && !isNaN(n))
return Math.max(...maxNum)
}


module.exports = findMax;
44 changes: 43 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,70 @@ 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, return-Infinity", () => {
const input = [];
const result = findMax(input);
expect(result).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 ine number, return-that number", () => {
const input = [10];
const result = findMax(input);
expect(result).toEqual(10);
});


// 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, return-the largest number", () => {
const input = [10, 70, 5, -30, 20, -10, -100];
const result = findMax(input);
expect(result).toEqual(70);
});

// 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 number", () => {
const input = [-10, -30, -5, -40, -50];
const result = findMax(input);
expect(result).toEqual(-5);
});


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

test("given an empty array, return-Infinity", () => {
const input = [0.1, 0.5, 0.03, 0.009, 0.9, 1.1, 7.9 ];
const result = findMax(input);
expect(result).toEqual(7.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("given an array, return-Infinity", () => {
const input = [10, 5, "banana", 60, "goat", "zebra", 5]
const result = findMax(input);
expect(result).toEqual(60)
});

// 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, return-Infinity", () => {
const input = ["car", "train", "bus", "bmw"]
const result = findMax(input);
expect(result).toEqual(-Infinity)
});
5 changes: 4 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function sum(elements) {
}
if (!Array.isArray(elements)) return null
const sumNum = elements.filter(n => typeof n === 'number' && !isNaN(n))
return sumNum.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
};

module.exports = sum;
31 changes: 30 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,53 @@ 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, return 0", () => {
const input = [];
const result = sum(input);
expect(result).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 one number", () => {
const input = [10];
const result = sum(input);
expect(result).toEqual(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 a negative number", () => {
const input = [-10, -20, -30, -40];
const result = sum(input);
expect(result).toEqual(-100);
});

// 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 number", () => {
const input = [0.1, 0.2, 0.3, 0.4];
const result = sum(input);
expect(result).toEqual(1);
});

// 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", () => {
const input = [10,"glasgow", 30, "london", 50, "asmara", 60, "massawa"];
const result = sum(input);
expect(result).toEqual(150);
});

// 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 containing non-number", () => {
const input = ["Nairobi","Glasgow", "London", "Asmara","Edinburgh", "Massawa"];
const result = sum(input);
expect(result).toEqual(0);
});
7 changes: 4 additions & 3 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// 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;
}
}


return false;
}

Expand Down
1 change: 1 addition & 0 deletions Sprint-1/refactor/includes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ test("searches for null", () => {

expect(currentOutput).toEqual(targetOutput);
});

10 changes: 10 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function calibrateFrequency(changesInTime) {
// we need to Start from a frequency of 0
let frequency = 0;

// Apply each change in order
for (let change of changesInTime) {
frequency += parseInt(change, 10);
}

return frequency;