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
19 changes: 16 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@
// 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;
// Return null for non arrays
if (!Array.isArray(list)) return null;
// Filter to only numeric values
const nums = list.filter((x) => typeof x === "number" && isFinite(x));
// Return null if no valid numbers remain
if (nums.length === 0) return null;
// Sort the numbers in ascending order
// filter already returns a new array so need to spread before spreading
const sorted = nums.sort((a, b) => a - b);
// Find the middle index of the sorted array
const middleIndex = Math.floor(sorted.length / 2);
// If odd length, return the middle index
// If even length, return the average of the two middle elements
return sorted.length % 2 !== 0
? sorted[middleIndex]
: (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
}

module.exports = calculateMedian;
10 changes: 9 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
function dedupe() {}
function dedupe(arr) {
// Return null if the input is not an array
if (!Array.isArray(arr)) return null;
// Use Set to remove duplicates,
// then spread back into a new array
return [...new Set(arr)];
}

module.exports = dedupe;
17 changes: 16 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@ 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("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, it returns a copy of the original array", () => {
const input = [1, 2, 3];
const result = dedupe(input);
// Check the contents match a hardcoded expected value, not the input itself
expect(result).toEqual([1, 2, 3]);
// Check it's a different array, not the same reference
expect(result).not.toBe(input);
});
Comment thread
cjyuan marked this conversation as resolved.
Comment on lines +26 to 33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your function is correct, but currently there is a chance that an incorrectly implemented function can pass this test. Can you figure out why?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

A function that just returns a copy without deduplicating (e.g. return [...arr]) would still pass since [1, 2, 3] has no duplicates.

I've added an extra check using an array with duplicates [1, 1, 2, 3] to make the test more robust.


// 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 duplicate numbers or strings, it removes duplicates preserving first occurence", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
4 changes: 4 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function findMax(elements) {
// Filter to only numeric values
const nums = elements.filter((x) => typeof x === "number" && isFinite(x));
// Math.max with no arguments returns -Infinity, which handles empty arrays as well
return Math.max(...nums);
}

module.exports = findMax;
25 changes: 24 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,51 @@ 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([])).toEqual(-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])).toEqual(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([30, 50, 10, 40])).toEqual(50);
expect(findMax([-10, 5, 3, -2])).toEqual(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 only negative numbers, returns the closest to zero", () => {
expect(findMax([-10, -3, -7])).toEqual(-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, return the largest decimal number", () => {
expect(findMax([1.5, 2.7, 0.3])).toEqual(2.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-numeric values, returns the max and ignores non-numerics", () => {
expect(findMax(["hey", 10, "hi", 60, 10])).toEqual(60);
// "300" looks numeric but is a string, so it should be ignored
expect(findMax([10, "300", 20])).toEqual(20);
});

// 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-numeric values, returns -Infinity", () => {
expect(findMax(["hey", "hi", null, undefined])).toEqual(-Infinity);
});
Comment thread
cjyuan marked this conversation as resolved.
5 changes: 5 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
function sum(elements) {
// Filter to only numeric values,
// then reduce to a total
return elements
.filter((x) => typeof x === "number" && isFinite(x))
.reduce((total, x) => total + x, 0);
}

module.exports = sum;
22 changes: 21 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,44 @@ 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("given an empty array, returns 0", () => {
expect(sum([])).toEqual(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([42])).toEqual(42);
});

// 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, -20, -30])).toEqual(-60);
expect(sum([-5, -3, -2])).toEqual(-10);
});

// 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 numbers, returns the correct sum", () => {
expect(sum([1.5, 2.5, 3.0])).toEqual(7);
// toBeCloseTo is used for floating point calculations to avoid precision issues
expect(sum([1.2, 0.6, 0.005])).toBeCloseTo(1.805);
});
Comment thread
cjyuan marked this conversation as resolved.

// 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-numeric values, ignores them and returns the sum", () => {
expect(sum(["hey", 10, "hi", 60, 10])).toEqual(80);
});

// 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-numeric values, returns 0", () => {
expect(sum(["hey", "hi", null, undefined])).toEqual(0);
});
6 changes: 4 additions & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// 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];
// Iterate directly over each element in the list
for (const element of list) {
// Return true as soon as we find a match
if (element === target) {
return true;
}
}
// Target was not found in the list
return false;
}

Expand Down
15 changes: 15 additions & 0 deletions Sprint-1/refactor/includes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,49 @@

const includes = require("./includes.js");

// Given an array containing the target value
// When passed to the includes function
// Then it should return true
test("returns true when target is in array", () => {
const currentOutput = includes(["a", "b", "c", "d"], "c");
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array that does not contain the target value
// When passed to the includes function
// Then it should return false
test("returns false when target not in array", () => {
const currentOutput = includes([1, 2, 3, 4], "a");
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array where the target appears more than once
// When passed to the includes function
// Then it should still return true
test("returns true when the target is in array multiple times", () => {
const currentOutput = includes([1, 2, 2, 3], 2);
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);
});

// Given an empty array
// When passed to the includes function
// Then it should return false
test("returns false for empty array", () => {
const currentOutput = includes([]);
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array containing null as an element
// When passed to the includes function with null as the target
// Then it should return true
test("searches for null", () => {
const currentOutput = includes(["b", "z", null, "a"], null);
const targetOutput = true;
Expand Down
Loading