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;

if (!Array.isArray(list)) {
return null;
}

const nums = list.filter(i => typeof i === "number" && !isNaN(i));

if (nums.length === 0) {
return null;
}

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

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


if (len % 2 === 1) {
return sorted[mid];
}

return (sorted[mid - 1] + sorted[mid]) / 2;
}

module.exports = calculateMedian;
10 changes: 9 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
function dedupe() {}
function dedupe(arr) {
return [...new Set (arr)];
}
console.log(dedupe([1, 2, 3]));
console.log(dedupe([1, 1, 2, 3, 2, 3, 3]));
console.log(dedupe([]));
console.log(dedupe(["hello", undefined, 5, 5, "hello"]));

module.exports = dedupe;
12 changes: 10 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ 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
// Then it should remove the duplicate values, preserving the first occurrence of each element
test("Given an array with strings or numbers, remove the duplicate values, preserving the first occurrence of each element", () => {
expect(dedupe(["hello", undefined, 5, 5, "hello"])).toEqual(["hello", undefined, 5,]);
});
16 changes: 15 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
function findMax(elements) {
}
const arr = elements.filter(i => typeof i === "number" && !isNaN(i));

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

let max = arr[0];

for (const num of arr) {
if (num > max) {
max = num;
}
}

return max;
}
module.exports = findMax;
28 changes: 22 additions & 6 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,44 @@ 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([50])).toBe(50);
})
// 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", () => {
expect(findMax([-1, -3, 5, 7, 8])).toBe(8);
});
// 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 closest to zero", () => {
expect(findMax([-7, -5, -2])).toBe(-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 decimal numbers, returns the largest decimal number", () => {
expect(findMax([2.34, 2.35])).toBe(2.35);
});
// 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-numbers, returns the max and ignore non-numeric values", () => {
expect(findMax(["hello", 2, 5, -1])).toBe(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
test("given an array with only non-numbers values, returns the largest surprising ", () => {
expect(findMax(["5", "hello", "a2"])).toBe(-Infinity);
});
17 changes: 16 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function sum(elements) {
function sum(elements) {
const numbers = elements.filter((i) => typeof i === "number" && !isNaN(i));

if (numbers.length === 0) {
return 0;
}

return numbers.reduce((total, num) => total + num, 0);
}

console.log(sum([10, 30, 45]));
console.log(sum([10]));
console.log(sum([-10, -30, -45]));
console.log(sum([]));
console.log(sum([10.5, 30, 45]));
console.log(sum([NaN, null, 45, 10]));
console.log(sum([NaN, null, "hello"]));

module.exports = sum;
23 changes: 18 additions & 5 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,37 @@ 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 just one number, returns that number", () => {
expect(sum([10, 20, 35])).toBe(65);
});
// 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, -30, -45])).toBe(-85);
});
// 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 correct total sum", () => {
expect(sum([10.5, 30, 45])).toBe(85.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, ignore the non-numerical values and return the sum of the numerical elements", () => {
expect(sum([NaN, null, 45, 10])).toBe(55);
});
// 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 the least surprising value", () => {
expect(sum([NaN, null, "hello"])).toBe(0);
});