Skip to content

London | 25-ITP-May | Hendrine Zeraua | Sprint 1 | Data Groups #635

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 6 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
45 changes: 40 additions & 5 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,45 @@
// 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).

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

module.exports = calculateMedian;


// I followed the instructions to fix the calculateMedian function:

// I started by attempting to run the tests in the Sprint-1/fix directory using npm test -- fix.
// After resolving setup issues and running npx jest, I reviewed the test failures.
// I then analysed the original code and realised it:
// - Didn’t sort the array before finding the median
// - Mutated the original list using .splice()
// - Didn’t handle non-numeric values or invalid inputs
// I rewrote the function to:
// - Filter out all non-numeric values (including NaN)
// - Handle edge cases like no numbers in the list or invalid input
// - Sort only the numeric values in ascending order
// - Correctly calculate the median for both even and odd-length arrays
// I avoided modifying the original input list by using the spread operator ([...numbers])
// I used the test results to confirm that the function now works as expected

function calculateMedian(list) {

if (!Array.isArray(list)) return null;

const numbers = list.filter(n => typeof n === 'number' && !isNaN(n));

if (numbers.length === 0) return null;

const sorted = [...numbers].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);

if (sorted.length % 2 === 0) {
return (sorted[mid - 1] + sorted[mid]) / 2;
} else {
return sorted[mid];
}
}
8 changes: 8 additions & 0 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
// passing all the tests...
// Fix the implementation of calculateMedian so it passes all tests

// All test cases are passing because:
// The calculateMedian function in median.js was updated to:
// • Filter out non-numeric values
// • Avoid mutating the input array
// • Correctly calculate medians for odd and even-length arrays
// • Handle unsorted and mixed-type inputs gracefully
// This confirms the function behaves as expected across a wide range of scenarios.

const calculateMedian = require("./median.js");

describe("calculateMedian", () => {
Expand Down
23 changes: 22 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
function dedupe() {}
//I used a Set to track seen values while looping to preserve order.
// It passes all test cases including edge cases like empty arrays and type-sensitive inputs.

function dedupe(array) {
if (!Array.isArray(array)) return [];

const seen = new Set();
const result = [];

for (const item of array) {
if (!seen.has(item)) {
seen.add(item);
result.push(item);
}
}

return result;
}

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.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 2: No duplicates
test("given an array with no duplicates, it returns a copy of the original array", () => {
expect(dedupe([1, 2, 3])).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("removes duplicates and preserves first occurrence (strings)", () => {
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
});

test("removes duplicates and preserves first occurrence (numbers)", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});

32 changes: 29 additions & 3 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
function findMax(elements) {
}
// function findMax(elements) {
// }

// module.exports = findMax;


module.exports = findMax;
// In my implementation of findMax:
// I first checked if the input is an array. If it isn’t, I return -Infinity as a safe fallback (e.g. for non-array inputs).
// Then, I filtered out all non-numeric values using .filter() and typeof el === "number", so that only valid numbers are considered.
// If there are no numeric values, I return -Infinity (which is consistent with how Math.max() behaves on an empty array).
// Finally, I used Math.max(...numbersOnly) with the spread operator to return the highest value from the filtered list.
// This approach ensures that the function handles:
// Empty arrays
// Arrays with all non-numbers
// Mixed arrays (e.g., numbers with strings or null)
// Positive, negative, and decimal numbers
// Without mutating the original array
// I also confirmed the function passes all tests, including edge cases.

function findMax(elements) {
if (!Array.isArray(elements)) return -Infinity;

const numbersOnly = elements.filter((el) => typeof el === "number");

if (numbersOnly.length === 0) return -Infinity;

return Math.max(...numbersOnly);
}

module.exports = findMax;
27 changes: 26 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,53 @@ 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.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([42])).toBe(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 both positive and negative numbers, returns the largest", () => {
expect(findMax([-10, 0, 20, -5, 15])).toBe(20);
});

// 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([-100, -1, -50])).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([1.1, 2.5, 3.9, 3.8])).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, ignores them and returns max", () => {
expect(findMax(["a", 1, "b", 3, null, 2])).toBe(3);
});


// 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 -Infinity", () => {
expect(findMax(["apple", null, undefined, {}, []])).toBe(-Infinity);
});

27 changes: 25 additions & 2 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
// function sum(elements) {
// }

// module.exports = sum;

/*
I implemented a function called sum that takes an array and returns
the total of all numeric values. I made sure to filter out any non-numeric
elements like strings or nulls.

I understand that:
- If the array is empty or has no numbers, it should return 0.
- It needs to handle negative numbers and decimal numbers correctly.
- Non-number values should be ignored to prevent errors or incorrect totals.
*/

function sum(elements) {
}
if (!Array.isArray(elements)) return 0;

return elements
.filter((el) => typeof el === "number")
.reduce((total, num) => total + num, 0);
}

module.exports = sum;


module.exports = sum;
24 changes: 22 additions & 2 deletions 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.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("given an array with negative numbers, returns correct sum", () => {
expect(sum([-5, -10, -15])).toBe(-30);
});

// 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 correct sum", () => {
expect(sum([1.5, 2.5, 3.25])).toBeCloseTo(7.25);
});

// 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, returns sum of numbers only", () => {
expect(sum(["apple", 10, null, 20, "banana", 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
test("given an array with only non-number values, returns 0", () => {
expect(sum(["cat", null, undefined, "dog"])).toBe(0);
});

28 changes: 26 additions & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
// 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];
// if (element === target) {
// return true;
// }
// }
// return false;
// }

// module.exports = includes;


/*

I refactored the includes function by changing the for loop to a for...of loop.
Originally, the function used a standard for loop with an index to go through the array.
I replaced that with a for...of loop, which lets me loop directly over each element in the list.
The logic stayed the same — I checked if the current element matches the target, and returned true if it did.
If the loop finishes without finding a match, the function returns false.
After making this change, I ran all the tests in includes.test.js, and they all passed.
This shows the function still works correctly after the refactor.

*/

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
3 changes: 2 additions & 1 deletion Sprint-1/refactor/includes.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Refactored version of includes should still pass the tests below:

// All tests passed after refactoring includes.js to use a for...of loop.
// These tests confirm the function still works correctly.
const includes = require("./includes.js");

test("returns true when target is in array", () => {
Expand Down
Empty file added mean.js
Empty file.
Empty file added mean.test.js
Empty file.