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
28 changes: 25 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,31 @@
// 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;
// Check if list is a valid array
if (!Array.isArray(list) || list.length === 0) {
return null;
}

// Filter only numeric values (numbers, not NaN, not null, not undefined)
const numbers = list.filter(item => typeof item === 'number' && !isNaN(item));

// If no valid numbers found, return null
if (numbers.length === 0) {
return null;
}

// Sort the numbers in ascending order (don't mutate original array)
const sorted = [...numbers].sort((a, b) => a - b);

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

// If odd length, return the middle element
if (sorted.length % 2 === 1) {
return sorted[middleIndex];
}

// If even length, return the average of the two middle elements
return (sorted[middleIndex - 1] + sorted[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 (!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;
15 changes: 14 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,28 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]

// 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");
test("given an empty array, it returns 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 with no duplicates, returns a copy of the original array", () => {
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]);
});
Comment on lines +27 to +30
Copy link
Contributor

Choose a reason for hiding this comment

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

The current test code cannot check if the returned array is a copy of the original array because toEqual() compares objects (including arrays) by value. To illustrate,

  const A = [2, 3, 1];
  const B = [...A];          // B is a copy of A
    
  // This set of code cannot distinguish if the compared objects are the same objects.
  expect(A).toEqual(A);  // true
  expect(A).toEqual(B);  // true

In order to check if the returned array is a copy of the original array, we would need additional checks.
Can you find out what code you need to add in order to ensure the returned value is not 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
test("given an array with duplicates, removes duplicates and preserves first occurrence", () => {
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]);
expect(dedupe(["x", "y", "x", "z", "y", "x"])).toEqual(["x", "y", "z"]);
});
24 changes: 23 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
function findMax(elements) {
}
// Filter only numeric values (numbers, not NaN, not null, not undefined)
const numbers = elements.filter(function(item) {
return typeof item === 'number' && !isNaN(item);
});

// Check if any numbers exist, Given an empty array, it should return -Infinity
if (numbers.length === 0) {
if (elements.length === 0) {
return -Infinity;
} else {
return null;
}
}
Comment on lines +8 to +14
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also consider an array with no numbers the same as an array with no elements. The latter also does not have any number.


// Find the maximum number
let max = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
return max;
}
Comment on lines +18 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you improve the indentation of this code?


module.exports = findMax;
34 changes: 32 additions & 2 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,59 @@ 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");
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([5])).toBe(5);
expect(findMax([42])).toBe(42);
expect(findMax([-10])).toBe(-10);
});

// 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([30, 50, 10, 40])).toBe(50);
expect(findMax([10, -5, 20, -15, 8])).toBe(20);
expect(findMax([-3, 5, -10, 2])).toBe(5);
});

// 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 closest to zero", () => {
expect(findMax([-5, -10, -3, -20])).toBe(-3);
expect(findMax([-100, -50, -1])).toBe(-1);
});

// 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([3.5, 2.1, 4.8, 1.2])).toBe(4.8);
expect(findMax([0.1, 0.9, 0.5])).toBe(0.9);
expect(findMax([10.5, 10.7, 10.3])).toBe(10.7);
});

// 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, ignores them and returns max", () => {
expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60);
expect(findMax([5, "hello", 15, null, 10, undefined])).toBe(15);
expect(findMax([1, "test", 2, NaN, 3])).toBe(3);
});

// 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 null", () => {
expect(findMax(["hello", "world"])).toBe(null);
expect(findMax([null, undefined, NaN])).toBe(null);
expect(findMax(["a", "b", "c"])).toBe(null);
});
4 changes: 2 additions & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +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];
for (const element of list) {
if (element === target) {
return true;
}
Expand Down