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
12 changes: 9 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
// 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((n) => typeof n === "number" && !isNaN(n));
if (!nums.length) return null;

const sorted = nums.slice().sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);

return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
}

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

const seen = new Set();
const result = [];

for (const item of arr) {
if (!seen.has(item)) {
seen.add(item);
result.push(item);
}
}

return result;
}

module.exports = dedupe;
7 changes: 7 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function findMax(elements) {
if (!Array.isArray(elements)) return -Infinity;

const nums = elements.filter((n) => typeof n === "number" && !isNaN(n));

if (nums.length === 0) return -Infinity;

return nums.reduce((max, curr) => (curr > max ? curr : max), -Infinity);
}

module.exports = findMax;
26 changes: 21 additions & 5 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([8])).toBe(8);
});
// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

test("returns the largest number from positive and negative numbers", () => {
expect(findMax([5, -9, 25, 8, -12])).toBe(25);
});
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

test("returns the closest to zero for all negative numbers", () => {
expect(findMax([-10, -2, -25])).toBe(-2);
});
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

test("returns the largest decimal number", () => {
expect(findMax([1.9, 2.9, 2.5])).toBe(2.9);
});
// 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("ignores non-numeric values", () => {
expect(findMax(["hey", 10, "hello", 50, 20])).toBe(50);
});

// 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("returns -Infinity for only non-number values", () => {
expect(findMax(["a", null, undefined])).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) {
if (!Array.isArray(elements)) return 0;

let total = 0;

for (const item of elements) {
if (typeof item === "number") {
total += item;
}
}

return total;
}

module.exports = sum;
18 changes: 9 additions & 9 deletions Sprint-1/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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