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
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to clone numbers?


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;
22 changes: 21 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,35 @@ 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"]);

// Reference copy check
const original = [7, 8, 9];
const result = dedupe(original);
expect(result).toEqual(original); // same values
expect(result).not.toBe(original); // different reference
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

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

Good job.


});

// 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"]);
});
20 changes: 19 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
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);
});

// Treat no numbers the same as empty input
if (numbers.length === 0) {
return -Infinity;
}

// 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;
}

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);
Comment on lines +61 to +63
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 include strings that can usually be safely converted to numbers (e.g., "100", "3e2") to ensure the function can properly ignore values that are not numbers.

});

// 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);
});
10 changes: 9 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function sum(elements) {
function sum(list) {
if (!Array.isArray(list)) return 0;
let total = 0;
for (const item of list) {
if (typeof item === 'number' && !isNaN(item)) {
total += item;
}
}
return total;
}

module.exports = sum;
21 changes: 18 additions & 3 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,44 @@ 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")
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([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 arrays with negative numbers", () => {
expect(sum([10, -5, -15, 20])).toBe(10);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("sums arrays with decimal numbers", () => {
expect(sum([1.5, 2.25, 3.25])).toBeCloseTo(7.0);
});

// 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(["a", 10, null, 5, "7", undefined, NaN, 3])).toBe(18);
});

// 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("array with only non-number values returns 0", () => {
expect(sum(["x", null, undefined, "y"])).toBe(0);
});
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
Loading