Skip to content

West Midlands | ITP-MAY25 | Saleh Yousef | Module-Data-Groups | Sprint-1 #551

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 5 commits into
base: main
Choose a base branch
from
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
19 changes: 15 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@
// 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).

// fixed




function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
if (!Array.isArray(list)) return null;
const numbers = list.filter((item) => typeof item === "number" && !isNaN(item));
if (numbers.length === 0) return null;
numbers.sort((a, b) => a - b);
const middleIndex = Math.floor(numbers.length / 2);
if (numbers.length % 2 === 0) {
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
} else {
return numbers[middleIndex];
}
}

module.exports = calculateMedian;
3 changes: 2 additions & 1 deletion Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe("calculateMedian", () => {
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
].forEach(({ input, expected }) =>
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
it(`filters out non-numeric values from [${input}] before calculating the median`, () => expect(calculateMedian(input)).toEqual(expected))

);
});
5 changes: 4 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
function dedupe() {}
function dedupe(arr) {
return Array.from(new Set(arr));
}
module.exports = dedupe;
11 changes: 10 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ 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, return original array", () => {
expect(dedupe([1, 2, 4, 6])).toEqual([1, 2, 4, 6]);
});

// 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, returns one element from each duplicated value", () => {
expect(dedupe([1, 2, "Moon", 3, 4, 1, "Moon", 4, 2])).toEqual([1, 2, "Moon", 3, 4]);
});
4 changes: 3 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function findMax(elements) {
const nums = elements.filter(n => typeof n === "number" && !isNaN(n));
return nums.length === 0 ? -Infinity : Math.max(...nums);
}

module.exports = findMax;
module.exports = findMax;
19 changes: 18 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,45 @@ 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 a one number [8] array, returns 8", () => {
expect(findMax([8])).toBe(8);
});

// 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 a positive and negative number, returns the largest one", () => {
expect(findMax([0, -1 ,-5])).toBe(0);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("given a negative numbers, returns teh closest to 0", () =>
{expect(findMax([-19, -40, -60])).toBe(-19)})

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("given a decimal numbers, returns teh largest number", () =>
{expect(findMax([1.9, 4.1, 6.0])).toBe(6.0)})

// 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 a number and non-number values, returns numbers and ignore non numeric values", () =>
{expect(findMax(["cake", 40, -93])).toBe(40)})

// 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 only non-number values, returns -Infinity", () => {
expect(findMax(["cake", "pie", null, undefined, {}, []])).toBe(-Infinity);
});
6 changes: 5 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function sum(elements) {
function sum(elements) {
return elements
.filter(n => typeof n === "number" && !isNaN(n))
.reduce((total, n) => total + n, 0);
Infinity

Choose a reason for hiding this comment

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

What is the Infinity doing on this line?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for catching that, it was just a stray line.

}

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

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("given a negative array, returns sum", () => {
expect(sum([-2, -9, -10])).toBe(-21);});

// 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, return total sum", () => {
expect(sum([1.8, 3, 5.2])).toBe(10);});

// 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, returns sum of the numerical elements", () => {
expect(sum(["madera", 9, 10])).toBe(19);});

// 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-number values, returns sum", () => {
expect(sum(["dublin", "dubai", "malaga"])).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) {
// Check if the current element is equal to the target
if (element === target) {
return true;
}
Expand Down