Skip to content

London | May-2025 | Halimatou Saddiyaa | Module-Data-Groups | Sprint 1 #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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;
// To correct the implementation with the test feedback, we need first to ensure that the function detects that list is an array of numbers and returns null if it is not.
if (!Array.isArray(list)) return null;

// Secondly, we need to implement a function that filter out non numeric values.
const numbers = list.filter((item) => typeof item === "number");

Choose a reason for hiding this comment

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

Excellent work. However there is an edge case that the tests don't require you to handle. You might want to look at the special NaN property and consider whether the code would work if this value appeared in one of the test arrays. As I say it is not required but might be something to watch out for in the future.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you, the code might not work with Nan as although it is a number it's not a valid numeric value for math operations.
I've corrected my function to include the case of NaN and added a test to check.

// Thirdly, we need to make sure that the function returns null if the list is empty or has no numeric values.
if (numbers.length === 0) {
return null;
}

// Then, we need to make sure that the function sorts the numbers in ascending order before calculating the median.
const sorted = [...numbers].sort((a, b) => a - b);
const middleIndex = Math.floor(sorted.length / 2);

// Finally, we need to make sure that the function returns the average of the two middle numbers if the list has an even number of elements.
if (sorted.length % 2 === 0) {
return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
} else {
return sorted[middleIndex];
}
}

//const middleIndex = Math.floor(list.length / 2);
//const median = list.splice(middleIndex, 1)[0];
//return median;}

module.exports = calculateMedian;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}
function dedupe(array) {
const seen = new Set();
return array.filter((item) => {
if (seen.has(item)) {
return false;
}
seen.add(item);
return true;
});
}

Choose a reason for hiding this comment

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

Clever solution. Using a Set is the right approach in most de-duplication problems. However there are easier ways to populate a Set from an Array. Do you need to use the filter function at all?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, I don't need to use the filter function as I can directly use Set and populate it from an Array. I've corrected my code to reflect that.


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 @@ -16,12 +16,25 @@ 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", () => {
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]);
});

// 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 strings or numbers, it removes duplicates preserving the first occurrence", () => {
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
expect(dedupe(["a", "b", "a", "c"])).toEqual(["a", "b", "c"]);
});
7 changes: 6 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
function findMax(elements) {
function findMax(arr) {
const numbers = arr.filter((item) => typeof item === "number");
if (numbers.length === 0) {
return -Infinity;
}
return Math.max(...numbers);
}
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.

Excellent solution.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks


module.exports = findMax;
31 changes: 30 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,57 @@ 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([12])).toBe(12);
});

// 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 both positive and negative numbers, returns the largest number overall", () => {
expect(findMax([-15, 30, 0, 10, -5])).toBe(30);
});

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

// 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 number", () => {
expect(findMax([1.8, 2.7, 0.3, 3.9])).toBe(3.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("given an array with non-number values, returns the max and ignores non-numeric values", () => {
expect(findMax(["hello", 10, "world", 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

// As this is a max function, it should return -Infinity as the least surprising value.

test("given an array with only non-number values, returns -Infinity", () => {
expect(findMax(["hello", "world", "hi", "there"])).toBe(-Infinity);
});
8 changes: 7 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function sum(elements) {
function sum(arr) {
return arr.reduce((total, current) => {
if (typeof current === "number") {
return total + current;
}
return total;
}, 0);
}

Choose a reason for hiding this comment

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

This is good. However it would be more typical pattern to use filter then reduce rather than having your 'filtering' logic within the reduce function. Why do you think that pattern would be preferred?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, I think using filter then reduce will be preferred because it separates the two steps and the code is then easier to read and understand. I've updated my code to reflect that.


module.exports = sum;
27 changes: 26 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,49 @@ 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([])).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 just one number, returns that number", () => {
expect(sum([11])).toBe(11);
});

// 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 containing negative numbers, returns the correct total sum", () => {
expect(sum([-10, -20, -30])).toBe(-60);
});

// 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/float numbers, returns the correct total sum", () => {
expect(sum([5.4, 2.3, 4.3])).toBe(12.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("given an array containing non-number values, ignores non-numerical values and returns the sum of numerical elements", () => {
expect(sum(["hello", 10, "world", 20, 30])).toBe(60);
});

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

//The least surprising value should be 0, as there are no numeric values to sum.

test("given an array with only non-number values, returns 0", () => {
expect(sum(["hello", "world", "hi", "there"])).toBe(0);
});
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