Skip to content

London | May-2025 | Halyna Kozlovska | Module Data Groups | Sprint 1 #542

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
14 changes: 11 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
// 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;
if (!Array.isArray(list)) return null;

const numbersList = list.filter(Number.isFinite).sort((a, b) => a - b);

const listLength = numbersList.length;
if (listLength === 0) return null;

const middleIndex = Math.floor(listLength / 2);
return listLength % 2 !== 0
? numbersList[middleIndex]
: (numbersList[middleIndex - 1] + numbersList[middleIndex]) / 2;
}

module.exports = calculateMedian;
11 changes: 10 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
function dedupe() {}
function dedupe(array) {
let result = [];

Choose a reason for hiding this comment

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

For very large arrays, a Set can provide better performance than using an Array. Consider updating this implementation to use a set. See here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

Copy link
Author

Choose a reason for hiding this comment

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

@AdnanGondal Thanks for your comment — good point! Using a Set makes lookups faster (O(1) vs O(n)) and keeps values unique, which is great for large arrays. Here the time and space difference is small because the dataset is tiny, but I’ll keep Set in mind for future cases where performance matters more.


for (const item of array) {
if (!result.includes(item)) result.push(item);
}

return result;
}
module.exports = dedupe;
12 changes: 11 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ 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");
describe("dedupeArray", () => {
test("given an empty array, return 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 a copy of the original array", () => {
expect(dedupe([1,2,3,4,5])).toEqual([1,2,3,4,5]);
});

// 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, remove the duplicate values, preserving the first occurence of each element", () => {
expect(dedupe([1, "test", 3, "test", 1])).toEqual([1, "test", 3]);
});
})
8 changes: 8 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function findMax(elements) {
let maxElement = -Infinity;

for (const element of elements) {
if (typeof element === "number" && !isNaN(element)) {
if (element > maxElement) maxElement = element;
}
}
return maxElement;
}

module.exports = findMax;
25 changes: 24 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,51 @@ 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");
describe("findMax", () => {
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, return that number", () => {
expect(findMax([3])).toBe(3)
});

// 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, return the largest number overall", () => {
expect(findMax([-5,47,100,-35])).toBe(100);
});

// 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", () => {
expect(findMax([-3,-7,-25,-77,-8])).toBe(-3);
});

// 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", () => {
expect(findMax([3.5, 8.6, 2.75, 75.25, 1.05])).toBe(75.25);
});

// 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, return the max and ignore non-numeric values", () => {
expect(findMax(["test", "hello world", 45, "undefined", 90, 4])).toBe(90);
});

// 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(["test", null, undefined, true])).toBe(-Infinity);
});
});
8 changes: 8 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function sum(elements) {
let sum = 0;

for (const element of elements) {
if (typeof element === "number" && !isNaN(element)) {
sum += element;
}
}
return Math.round(sum * 100) / 100;;

Choose a reason for hiding this comment

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

Can you tell me what this line does?

Copy link
Author

Choose a reason for hiding this comment

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

@AdnanGondal Line 9 rounds the total sum to two decimal places before returning it.

}

module.exports = sum;
27 changes: 23 additions & 4 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,43 @@ 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")
describe("sumElements", () => {
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
// Then it should return that number
test("given an array with just one number, return that number", () => {
expect(sum([7])).toBe(7);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
// Then it should still return the correct total sum
test("given an array containing negative numbers, return the correct total sum", () => {
expect(sum([-5, -15, -4, -25])).toBe(-49);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
// Then it should return the correct total sum
test("given an array with decimal/float numbers, return the correct total sum", () => {
expect(sum([2.5, 15.75, 20.15, 1.55])).toBeCloseTo(39.95, 2);
});

// 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, ignore the non-numerical values and return the sum of the numerical elements", () => {
expect(sum(["text", 17, "undefined", 23])).toBe(40);
});

// 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, return the least surprising value given how it behaves for all other inputs", () => {
expect(sum(["test", null, undefined, true, "25"])).toBe(0);
});
})
7 changes: 2 additions & 5 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,11 +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];
if (element === target) {
return true;
}
for (const item of list) {
if (item === target) return true;
}
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions Sprint-1/refactor/includes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ test("returns true when target is in array", () => {
const currentOutput = includes(["a", "b", "c", "d"], "c");
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);
expect(currentOutput).toBe(targetOutput);
});

test("returns false when target not in array", () => {
const currentOutput = includes([1, 2, 3, 4], "a");
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
expect(currentOutput).toBe(targetOutput);
});

test("returns true when the target is in array multiple times", () => {
const currentOutput = includes([1, 2, 2, 3], 2);
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);
expect(currentOutput).toBe(targetOutput);
});

test("returns false for empty array", () => {
const currentOutput = includes([]);
const currentOutput = includes([], "test");
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
expect(currentOutput).toBe(targetOutput);
});

test("searches for null", () => {
const currentOutput = includes(["b", "z", null, "a"], null);
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);
expect(currentOutput).toBe(targetOutput);
});