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
25 changes: 16 additions & 9 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// Fix this implementation
// Start by running the tests for this function
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory
function calculateMedian(list) {
// Check if input is a valid array
if (!Array.isArray(list)) return null;

// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
// or 'list' has mixed values (the function is expected to sort only numbers).
// Filter out only numeric values
const nums = list.filter(val => typeof val === 'number' && !isNaN(val));
if (nums.length === 0) return null;

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
// Clone and sort the array numerically
const sorted = [...nums].sort((a, b) => a - b);
Comment on lines +9 to +10

Choose a reason for hiding this comment

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

Overall this is excellent. But is it necessary to clone the array here? Could we just sort it?

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

// Return median based on even or odd length
if (sorted.length % 2 !== 0) {
return sorted[mid];
} else {
return (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;
}

Comment on lines +1 to +16

Choose a reason for hiding this comment

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

Very good. Using a Set is the right approach. However there are easier ways in Javascript to populate a Set from an array. Do we need a for loop at all?

module.exports = dedupe;
33 changes: 31 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,41 @@ 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.todo("given an empty array, it returns an empty array");
test("returns an empty array when given 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 of numbers with no duplicates, it returns a copy of the original array", () => {
const input = [1, 2, 3];
const result = dedupe(input);
expect(result).toEqual([1, 2, 3]);
expect(result).not.toBe(input); // Ensure it's a new array, not the same reference
});

test("given an array of strings with no duplicates, it returns a copy of the original array", () => {
const input = ["a", "b", "c"];
const result = dedupe(input);
expect(result).toEqual(["a", "b", "c"]);
expect(result).not.toBe(input);
});

// 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("removes duplicate numbers, preserving the first occurrence", () => {
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});

test("removes duplicate strings, preserving the first occurrence", () => {
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
expect(dedupe(["x", "y", "x", "z", "y"])).toEqual(["x", "y", "z"]);
});

test("handles mixed types (strings and numbers)", () => {
expect(dedupe(["1", 1, "1", 2, 2, "2"])).toEqual(["1", 1, 2, "2"]);
expect(dedupe([1, "1", 1, "1"])).toEqual([1, "1"]);
});
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(val => typeof val === 'number' && !isNaN(val));

return nums.length > 0 ? Math.max(...nums) : -Infinity;
}

module.exports = findMax;
28 changes: 24 additions & 4 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,48 @@ 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.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([42])).toBe(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("given an array with positive and negative numbers, returns the largest", () => {
expect(findMax([-10, 5, 20, -3])).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 only negative numbers, returns the closest to zero", () => {
expect(findMax([-100, -20, -3, -50])).toBe(-3);
});

// 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", () => {
expect(findMax([1.2, 3.5, 2.9, 3.6])).toBe(3.6);
});

// 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 of valid numbers", () => {
expect(findMax(["a", 10, "b", 20, null, undefined, NaN, 5])).toBe(20);
expect(findMax([false, 3, true, 7, "hello"])).toBe(7);
});

// 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(["a", null, undefined, NaN, {}, [], "b"])).toBe(-Infinity);
expect(findMax([false, true, "zero", "1", {}, []])).toBe(-Infinity);
});
7 changes: 6 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
function sum(elements) {
if (!Array.isArray(elements)) return 0;

return elements.reduce((acc, val) => {
return typeof val === "number" && !isNaN(val) ? acc + val : acc;
}, 0);
}
Comment on lines 1 to 7

Choose a reason for hiding this comment

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

Very good. However using filter() then reduce() is a more common approach to this type of problem. What are the advantages of using this pattern rather than having the 'filtering logic' within the reduce() function?


module.exports = sum;
module.exports = sum;
23 changes: 19 additions & 4 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,39 @@ 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.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 one number, returns that number", () => {
expect(sum([42])).toBe(42);
});
// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

test("sums array with negative numbers", () => {
expect(sum([-1, -2, 3, 4])).toBe(4);
});
// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("sums array with decimals", () => {
expect(sum([1.5, 2.5, 3])).toBe(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("ignores non-number values", () => {
expect(sum(["hello", 10, null, 20, undefined, 5])).toBe(35);
});

// 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("returns 0 for array with only non-number values", () => {
expect(sum(["hello", null, undefined, {}, [], "42"])).toBe(0);
});
5 changes: 1 addition & 4 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// 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