Skip to content

WESTMIDLANDS | ITP-MAY-25 | AHMADEHSAS | SPRINT-2 | DATA GROUPS #756

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 13 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
23 changes: 20 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,26 @@
// 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; // Return null if input is not an array
}
// Filter only numeric values
const filteredlist = list.filter((item) => typeof item === "number");

// Return null if no numeric values are present
if (filteredlist.length === 0) {
return null;
}
// sort the numbers
filteredlist.sort((a, b) => a - b);
const middleIndex = Math.floor(filteredlist.length / 2);

// calculate and return median
if (filteredlist.length % 2 === 0) {
return (filteredlist[middleIndex - 1] + filteredlist[middleIndex]) / 2;
} else {
return filteredlist[middleIndex];
}
}

module.exports = calculateMedian;
16 changes: 15 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
function dedupe() {}
function dedupe(array) {
if (!Array.isArray(array)) {
return [];
}

const result = [];
for (let item of array) {
if (!result.includes(item)) {
result.push(item);
}
}
return result;
}

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

test("given an array with no duplicates, it returns the same array", () => {
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]);
});

test("removes duplicate from an array of numbers", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});

test("removes duplicate from an array of strings", () => {
expect(dedupe(["a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
});

test("preserves the order of first occurrences", () => {
expect(dedupe([3, 1, 2, 2, 1])).toEqual([3, 1, 2]);
});

test("handles mixed types correctly", () => {
expect(dedupe([1, "1", 1, "1"])).toEqual([1, "1"]);
});

// Given an array with no duplicates
// When passed to the dedupe function
Expand Down
11 changes: 11 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
function findMax(elements) {
const numericElements = elements.filter(
(elements) => typeof elements === "number"
); // filter out only numeric values

// If there are no numeric values, return infinity
if (numericElements.length === 0) {
return Infinity;
}

// use Math.max with spread (...) to get the maximum number
return Math.max(...numericElements);
}

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

test("given an array with one number, return that number", () => {
expect(findMax([42])).toBe(42);
});

test("given positive and negative numbers, return the largest number", () => {
expect(findMax([3, -10, 20, 7, -4])).toBe(20);
});

test("given only negative numbers, return the closest one to zero", () => {
expect(findMax([-5, -10, -3, -8])).toBe(-3);
});

test("given decimal numbers, return the largest decimal number", () => {
expect(findMax([1.5, 2.3, 0.4, 0.1])).toBe(2.3);
});

test("ignores non-numeric values", () => {
expect(findMax([10, "a", true, 50, null, 40])).toBe(50);
});

test("given only non-number values, returns -Infinity", () => {
expect(findMax(["a", "h", undefined, null])).toBe(Infinity);
});

// Given an array with one number
// When passed to the max function
Expand Down
5 changes: 5 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
function sum(elements) {
// Filter for numbers only
const numericValues = elements.filter((item) => typeof item === "number");

// Add the numbers
return numericValues.reduce((total, num) => total + num, 0);
}

module.exports = sum;
21 changes: 19 additions & 2 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ 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);
});

test("given an array with one number, return that number", () => {
expect(sum([42])).toBe(42);
});

// Given an array with just one number
// When passed to the sum function
Expand All @@ -22,15 +28,26 @@ test.todo("given an empty array, returns 0")
// 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 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 numbers, returns the correct total sum", () => {
expect(sum([1.5, 2.5, 3.5])).toBe(7.5);
});

// 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("ignores non-numeric values", () => {
expect(sum([10, "a", true, 50, null, 40])).toBe(100);
});
// 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(["a", "b", null, true])).toBe(0);
});
Loading