Skip to content
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) || list.length === 0) {
return null;
}

const numbers = list.filter((item) => typeof item === "number");

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

numbers.sort((a, b) => a - b);

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

if (numbers.length % 2 !== 0) {
const median = numbers.splice(middleIndex, 1)[0];
return median;
} else {
const middleTwo = numbers.splice(middleIndex - 1, 2);
const median = (middleTwo[0] + middleTwo[1]) / 2;
return median;
}
}

module.exports = calculateMedian;
6 changes: 5 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
function dedupe() {}
function dedupe(list) {
return [...new Set(list)];
}

module.exports = dedupe;
37 changes: 25 additions & 12 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,31 @@ E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8]
E.g. dedupe([1, 2, 1]) returns [1, 2]
*/

// Acceptance Criteria:
describe("dedupe", () => {
// Acceptance Criteria:

// 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");
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
test("should return an empty array when passed 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
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("should return a copy of the original array when there are no duplicates", () => {
const input = [1, 2, 3];
expect(dedupe(input)).toEqual(input);
});

// 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
// first occurrence of each element from the original array.
// 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
// first occurrence of each element from the original array.
test("should return a new array with duplicates removed while preserving the first occurrence of each element", () => {
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
});
9 changes: 9 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function findMax(elements) {
const numbersOnly = elements.filter(
(element) => typeof element === "number" && !isNaN(element)
);

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

return Math.max(...numbersOnly);
}

module.exports = findMax;
82 changes: 53 additions & 29 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,56 @@ We have set things up already so that this file can see your function from the o

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");

// Given an array with one number
// When passed to the max function
// Then it should return that number

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

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

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values

// 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
describe("findMax", () => {
// 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("should return -Infinity when passed 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("should return the only number when passed an array with one number", () => {
expect(findMax([42])).toEqual(42);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test("should return the largest number overall when passed an array with both positive and negative numbers", () => {
expect(findMax([-10, 0, 5, -20, 15])).toEqual(15);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("should return the closest number to zero when passed an array with just negative numbers", () => {
expect(findMax([-10, -5, -20, -1])).toEqual(-1);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("should return the largest decimal number when passed an array with decimal numbers", () => {
expect(findMax([1.5, 2.3, 0.8, 3.1])).toEqual(3.1);
});

// 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("should return the max and ignore non-numeric values when passed an array with non-number values", () => {
expect(findMax(["hey", 10, "hi", 60, 10])).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("should return -Infinity when passed an array with ONLY non-number values", () => {
expect(findMax(["hey", "hi", "hello"])).toEqual(-Infinity);
});
});
8 changes: 8 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function sum(elements) {
const numbersOnly = elements.filter(
(element) => typeof element === "number" && !isNaN(element)
);

if (numbersOnly.length === 0) {
return 0;
}
return Number(numbersOnly.reduce((acc, curr) => acc + curr, 0).toFixed(1));
}

module.exports = sum;
19 changes: 18 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,41 @@ 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("should return 0 when passed 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("should return the only number when passed an array with one number", () => {
expect(sum([42])).toEqual(42);
});

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

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

// 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("should ignore non-numerical values and return the sum of the numerical elements", () => {
expect(sum(["hey", 10, "hi", 60, 10])).toEqual(80);
});

// 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("should return 0 when passed an array with ONLY non-number values", () => {
expect(sum(["hey", "hi", "hello"])).toEqual(0);
});
Loading
Loading