Skip to content

London | May-2025 | Ikenna Agulobi | Data Groups | sprint-1 #575

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 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2f37fe6
I implemented calculate median function to return the median of a so…
ike-agu Jul 10, 2025
e110223
I edited calculate median function to return filtered numbers from …
ike-agu Jul 10, 2025
ab874b4
no relevant impact changes
ike-agu Jul 10, 2025
997ad4f
added coding exercises I practiced for my prep
ike-agu Jul 10, 2025
51955ab
Fixed the function to return null if the input provided is not an array
ike-agu Jul 10, 2025
aed7949
Implemented dedupe function and implemented first test case if array …
ike-agu Jul 11, 2025
62a5f73
implemented test to check that array with no duplicates returns copy …
ike-agu Jul 11, 2025
d8ab058
implemented test to check that array with either strings or numbers, …
ike-agu Jul 11, 2025
28b37ee
implemented test to return -Infinity is passed empty array
ike-agu Jul 11, 2025
c8db981
Implemented findMax function
ike-agu Jul 11, 2025
bf98503
Implemented test Given an array with one number, return that number
ike-agu Jul 11, 2025
5ad878d
Implemented test Given an array with both positive and negative numbe…
ike-agu Jul 11, 2025
df4c578
implemented to return largest negative number, largest decimal number
ike-agu Jul 11, 2025
e057adb
fixed findMax function to extract numbers from the Array incase array…
ike-agu Jul 11, 2025
bda9ea1
Implemented Sum function
ike-agu Jul 11, 2025
ee338d1
added test case to return 0 for empty array. Fixed sum function to re…
ike-agu Jul 11, 2025
d6d520d
Implemented remaining test cases for sum function
ike-agu Jul 11, 2025
c7d1caf
implemented test case to return -Infinity for non-number values
ike-agu Jul 11, 2025
b226613
fixed the implementation of includes to use a for...of loop
ike-agu Jul 12, 2025
7c847c5
fixed the function to find the mean of a list based on array length
ike-agu Jul 12, 2025
9c35969
Update prep-for-data-groups-module/arrays-practice.js
ike-agu Jul 12, 2025
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
27 changes: 24 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@
// 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;
//Checking the input provided to ensure it's an Array
if (!Array.isArray(list)) {
return null;
}
//Filtering out numbers from the array without changing the original array. Creates new array filteredNumbersOnly!
const filteredNumbersOnly = list.filter(
(element) => typeof element === "number" && !isNaN(element)

Choose a reason for hiding this comment

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

This is correct! Do you understand why we also need !isNaN here?

Copy link
Author

Choose a reason for hiding this comment

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

Yes! We need !isNaN in my function so that it filters real usable numbers and ignore anything that is not a real number like 'NaN".

);
//if the array has no valid numbers, it returns null
if (filteredNumbersOnly.length === 0) {
return null;
}
//Sorting numbers in the array from the ascending order. Spread operator creates new array to avoid changing the original one.
const sortedList = [...filteredNumbersOnly].sort((a, b) => a - b);
//If the array length is odd, return the middle number
if (sortedList.length % 2 === 1) {
const middleIndex = Math.floor(sortedList.length / 2);
return sortedList[middleIndex];
} else {
// If the array length is even, return the average of the two middle numbers.
const midddleNum1 = sortedList[sortedList.length / 2 - 1];
const midddleNum2 = sortedList[sortedList.length / 2];
return (midddleNum1 + midddleNum2) / 2;
}
}

module.exports = calculateMedian;
8 changes: 7 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
function dedupe() {}
function dedupe(arr) {
const newArray = [... new Set(arr)]
return newArray;
};
Comment on lines +1 to +4

Choose a reason for hiding this comment

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

have you tested this? does it run or is there a syntax error?

Do you understand how the Set works?

Copy link
Author

Choose a reason for hiding this comment

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

I tested my code and it does work.
Set() is a JS object which stores collection of unique values. For instance, in my code "new Set(arr) " filters out any duplicates in the array and creates a set of new values with no duplicates. Then the spread operator converts it back to a new array .


// console.log(dedupe(["b", "c", "a", "b", "c", 1, "a", 2]));
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", () => {
let emptyArr = []
let result = dedupe(emptyArr);
expect(result).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 should return a copy of the original array", () => {
let arrayWithNoDuplicates = [1, 2, 3];
let result = dedupe(arrayWithNoDuplicates);
expect(result).toEqual([1, 2, 3]);
});


// 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", () => {
let arrayNumsOrStrings = ["b", "c", "a", "b", "c", 1, "a", 2];
let result = dedupe(arrayNumsOrStrings);
expect(result).toEqual(["b", "c", "a", 1, 2]);
});
17 changes: 17 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
function findMax(elements) {
let numbers = [];
// using for loop to extract numbers from the Array incase array is mixed
for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number") {
numbers.push(elements[i]);
}
}
// Return the largest number, else -Infinity if no numbers are found
if (numbers.length > 0) {
return Math.max(...numbers);
} else {
return -Infinity;
}
}

// console.log(findMax([1, 2, 3, 4, 10]));
// console.log(findMax([]));
// console.log(findMax([1]));
// console.log(findMax( [4, "hello", "fruit", 2, 5]))
module.exports = findMax;
34 changes: 33 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,60 @@ 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",() => {
let emptyArray = []
let result = findMax(emptyArray);
expect(result).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, return that number", () => {
let oneNumArray = [12];
let result = findMax(oneNumArray);
expect(result).toEqual(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, it should return the largest number overall", () => {
let positiveAndNegativeNum= [-2, 3, 4,-5];
let result = findMax(positiveAndNegativeNum);
expect(result).toEqual(4);
});

// 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, return the closest one to zero", () => {
let negativeNumArray = [-1, -2, -3];
let result = findMax(negativeNumArray);
expect(result).toEqual(-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, return the largest decimal number", () => {
let decimalNumArray = [1.5, 2.5, 2.2];
let result = findMax(decimalNumArray);
expect(result).toEqual(2.5);
});

// 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, Then it should return the max and ignore non-numeric values", () => {
let mixedArray = [4, "hello", "fruit", 2, 5];
let result = findMax(mixedArray);
expect(result).toEqual(5);
});

// 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, return the least surprising value given how it behaves for all other inputs", () => {
expect(findMax(["apple", null, undefined, {}, []])).toEqual(-Infinity);
});
17 changes: 17 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
function sum(elements) {
let onlyNumbers = [];
//Extract numbers from the Array incase array is mixed
for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number") {
onlyNumbers.push(elements[i]);
}
}
if (onlyNumbers.length > 0) {
return onlyNumbers.reduce((a, b) => a + b);
} else {
return 0;
}
}

console.log(sum([10, 20, 30]));
console.log(sum(["hey", 10, "hi", 60, 10]));
console.log(sum([]));


module.exports = sum;
24 changes: 23 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,46 @@ 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", () => {
let emptyArray = [];
let result = sum(emptyArray)
expect(result).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, it should return that number", () => {
let oneNumArray = [23];
let result = sum(oneNumArray);
expect(result).toBe(23);
});

// 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, it should still return the correct total sum", () => {
expect(sum([-2, -4, -1])).toBe(-7);
});

// 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, it should return the correct total sum", () => {
expect(sum([2.5, 4.5, 1.1])).toBe(8.1);
});

// 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, return the sum of the numerical elements", () => {
expect(sum(["hey", 10, "hi", 60, 10])).toBe(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-number values, should return 0", () => {
expect(sum(["apple", null, undefined, {}, []])).toBe(0);
});
Loading