Skip to content
Open
23 changes: 20 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,26 @@
// 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) => typeof el === "number" && Number.isFinite(el)

Choose a reason for hiding this comment

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

According to
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite

isFinite already checks if the element is a number ( typeof el === "number" ).

I like the usage of the method tho.

);

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?

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);
});
});
15 changes: 15 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function sum(elements) {
const numbers = elements.filter(
(item) => typeof item === "number" && !isNaN(item)
);

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

const maxDecimalPlaces = numbers.reduce(
(currMax, el) => Math.max(currMax, (String(el).split(".") || "").length),
0
);
const decMultiplier = 10 ** maxDecimalPlaces;

Choose a reason for hiding this comment

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

This is a very robust solution, but too complex for the sake of the learning purposes, could we assume that the max float decimal places are 2 ? as we see in tests ?

https://github.com/CodeYourFuture/Module-Data-Groups/pull/896/files#diff-b88d7772fc056e6e9f3a6eb58f5cc851f9187a7f26f8f979a80e070bf79bf1bdR38


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

return Math.round((total + Number.EPSILON) * decMultiplier) / decMultiplier;

Choose a reason for hiding this comment

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

Therefore as a consequence of the comment above, we will not need Epsilon here as MathRound will do its work as expected.

}

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