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
26 changes: 22 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,27 @@
// 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;
list = list || [];
let newOrder = Array.from(list);
let i = 0;
while (i < newOrder.length) {
if (typeof newOrder[i] !== "number") newOrder.splice(i, 1);
else i++;
}
if (newOrder.length === 0) {
return null;
}
newOrder = newOrder.sort((a, b) => a - b);
const middleIndex = Math.floor(newOrder.length / 2);
if (newOrder.length % 2 !== 0) {
return newOrder[middleIndex];
} else {
const middleTwo = (newOrder[middleIndex] + newOrder[middleIndex - 1]) / 2;
return middleTwo;
}
}

module.exports = calculateMedian;
console.log(calculateMedian([110, 20, 0]));
console.log(calculateMedian([3, "apple", 1, 2, undefined, 4]));
console.log(calculateMedian([]));
console.log(calculateMedian(["frog", "dog", "log", "cog"]));
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(elements) {
const noDupe = [...new Set(elements)];
return noDupe;
}
console.log(dedupe([3, 3, 3, 4, 5, 4, "cat", "dog", "cat", 6, 1, 2, 3, 1]));
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("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]);
expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]);
});

// 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 duplicates preserving first occurrence", () => {
expect(dedupe(["a", "a", "b", "c", "c"])).toEqual(["a", "b", "c"]);
expect(dedupe([1, 2, 2, 3, 4, 4])).toEqual([1, 2, 3, 4]);
expect(dedupe([4, 6, "hi", 4, 6, "hi"])).toEqual([4, 6, "hi"]);
});
6 changes: 6 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function findMax(elements) {
const numbers = elements.filter((element) => typeof element === "number");
if (numbers.length === 0) {
return -Infinity;
}
numbers.sort((a, b) => a - b);
return numbers[numbers.length - 1];
}

module.exports = findMax;
34 changes: 31 additions & 3 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,57 @@ 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");
test("Given an empty array Then it should return -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])).toEqual(2);
expect(findMax([7])).toEqual(7);
expect(findMax([-7])).toEqual(-7);
expect(findMax([70000000])).toEqual(70000000);
});

// 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 Then it should return the largest number overall", () => {
expect(findMax([2, -9])).toEqual(2);
expect(findMax([2, -4, -2, 9])).toEqual(9);
});

// 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 just negative numbers Then it should return the closest one to zero", () => {
expect(findMax([-2, -4, -1, -99])).toEqual(-1);
expect(findMax([2])).toEqual(2);
});

// 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 just negative numbers Then it should return the closest one to zero", () => {
expect(findMax([3.7, 3.8, 3.9])).toEqual(3.9);
expect(findMax([2.8, 2.6, 2.5, 2.1])).toEqual(2.8);
});

// 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 just negative numbers Then it should return the closest one to zero", () => {
expect(findMax([-2, -4, -1, "p"])).toEqual(-1);
expect(findMax([2, 5, "i", 3, "r"])).toEqual(5);
});

// 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
// Then it should return the least surprising value given how it behaves for all other input
test("Given an array with only non-number values Then it should return the least surprising value given how it behaves for all other input", () => {
expect(findMax(["cat"])).toEqual(-Infinity);
expect(findMax(["cat", "dog"])).toEqual(-Infinity);
expect(findMax(["cat", "dog", "catdog"])).toEqual(-Infinity);
});
9 changes: 8 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function sum(elements) {
}
let total = 0;
const numbers = elements.filter((element) => typeof element === "number");

numbers.forEach((numb) => {
total = total + numb;
});
return total;
}
console.log(sum([5]));
module.exports = sum;
23 changes: 22 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,45 @@ 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 Then it should return 0", () => {
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 one element it should return that number", () => {
expect(sum([4])).toEqual(4);
expect(sum([10000])).toEqual(10000);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("Given negitave numbers still sums correctly", () => {
expect(sum([2, 3, -3])).toEqual(2);
expect(sum([5, -3, -3])).toEqual(-1);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("Given a float number it still sums correctly", () => {
expect(sum([4.2, 4.3])).toEqual(8.5);
expect(sum([2.223, 5.345, 3.141])).toEqual(10.709);
expect(sum([3.887, 2.1945, -3.5])).toEqual(2.5815);
});

// 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 non-numerical values should ignore and sum the numbers", () => {
expect(sum([4, "four", "fore", "iv", 4])).toEqual(8);
});

// 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 only non-numerical values should return 0", () => {
expect(sum(["tommorow", "and", "tommorow", "and", "tommorow"]));
});
3 changes: 1 addition & 2 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

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
Loading