Skip to content
Closed
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
21 changes: 18 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,24 @@
// 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 filteredNumericArr = list.filter((el) => Number.isFinite(el));

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

const sortedArr = [...filteredNumericArr].sort((a, b) => a - b);
const middleIndex = Math.floor(sortedArr.length / 2);

if (sortedArr.length % 2 !== 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this last logic from 24 to 28 lines?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because when the list has an even number of numbers, there isn’t just one middle value, but there are two. In that situation, the median is defined as the average of those two middle numbers.

return sortedArr[middleIndex];
} else {
return (sortedArr[middleIndex - 1] + sortedArr[middleIndex]) / 2;
}
}

module.exports = calculateMedian;
16 changes: 15 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
function dedupe() {}
function dedupe(arr) {
if (arr.length === 0) return [];

const noDupsArr = [];
const seenItems = new Set();

for (const el of arr) {
if (!seenItems.has(el)) {
noDupsArr.push(el);
seenItems.add(el);
}
}
return noDupsArr;
}
module.exports = dedupe;
33 changes: 22 additions & 11 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,27 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
*/

// Acceptance Criteria:
describe("dedupe", () => {
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
test("given an empty array, returns an empty array", () => {
expect(dedupe([])).toEqual([]);
});

// 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 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, returns a copy of the original array", () => {
const input = [1, 65, 2, 298, 3, 729];
expect(dedupe(input)).toEqual([1, 65, 2, 298, 3, 729]);
});

// 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 strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurence of each element
// 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 duplicates, removes duplicate values preserving the first occurrence", () => {
const input = ["a", "b", "a", "c", "b", "d", "e", "d", "c", "a", "b"];
expect(dedupe(input)).toEqual(["a", "b", "c", "d", "e"]);
});
});
12 changes: 12 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
function findMax(elements) {
if (elements.length === 0) {
return -Infinity;
} else if (elements.length === 1 && typeof elements[0] === "number") {
return elements[0];
}
const maxEl = Math.max(
...elements.filter((el) => typeof el === "number" && !isNaN(el))
);

if (elements.length > 1) {
return maxEl;
}
}

module.exports = findMax;
81 changes: 52 additions & 29 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,55 @@ 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
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([15])).toBe(15);
});

// 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, returns the largest number", () => {
expect(findMax([-112, 7, 3, 20, -2])).toBe(20);
});

// 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, returns the largest number - the closest one to zero", () => {
expect(findMax([-653, -80, -7, -4, -12])).toBe(-4);
});

// 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([27.3, 654.1, 45.4, 325.9])).toBe(654.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("given an array with non-number values, returns the max and ignores non-numeric values", () => {
expect(
findMax([7, {}, "Greetings!", 34, null, 15.3, undefined, 30, 3878, NaN])
).toBe(3878);
});

// 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-number values, returns -Infinity", () => {
expect(findMax([{}, "Hello!", null, undefined, NaN])).toBe(-Infinity);
});
});
11 changes: 11 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
function sum(elements) {
const numbers = elements.filter(
(item) => typeof item === "number" && !isNaN(item)
);

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

const total = numbers.reduce((acc, el) => acc + el, 0);

const decMultiplier = 100;

return Math.round(total * decMultiplier) / decMultiplier;
}

module.exports = sum;
68 changes: 43 additions & 25 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,46 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
const sum = require("./sum.js");

// Acceptance Criteria:

// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")

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

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

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

// 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

// 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
describe("sum", () => {
// Given an empty array
// When passed to the sum function
// Then it should return 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 one number, returns that number", () => {
expect(sum([15])).toBe(15);
});

// 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 with negative numbers, returns the correct sum", () => {
expect(sum([10, -6, 18, -10])).toBe(12);
});

// 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([65.5, 8.45, 32.4, 97.36, 10])).toBe(213.71);
});

// 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 with non-number values, ignores them and returns the sum of numerical elements", () => {
expect(sum([10, "hello", 25, null, 5, undefined, {}, 15])).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 0", () => {
expect(sum(["Great!", "hi", null, undefined, {}, []])).toBe(0);
});
});
5 changes: 2 additions & 3 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,9 +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];
if (element === target) {
for (const el of list) {
if (el === target) {
return true;
}
}
Expand Down