Skip to content

ZA-ITP-May-2025 | Christian Mayamba | Module-Data-Groups | Week 1 #573

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 1 commit
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
16 changes: 16 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
function findMax(elements) {
if (elements.length === 0) {
return "-Infinity";
}

if (elements.every((item) => typeof item === "string")) {
Copy link
Member

Choose a reason for hiding this comment

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

Same question as before - is "I can't find the max of an array of only strings" really the general statement here?

return "undefined";
Copy link
Member

Choose a reason for hiding this comment

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

Returning the string "undefined" seems like an odd choice here. Can you think of a more appropriate value to return?

}

const numbersArray = elements.filter((item) => typeof item === "number");
elements.sort((a, b) => a - b);
const maxNum = elements[elements.length - 1];

return maxNum;
}
const verArray = [-5, -10, -65, -6];

console.log(findMax(verArray));

module.exports = findMax;
22 changes: 21 additions & 1 deletion Sprint-1/implement/max.test.js
Copy link

Choose a reason for hiding this comment

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

Tests passed, well done

Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,48 @@ 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("Empty array should return -Infinity", () => {
expect(findMax([])).toEqual("-Infinity");
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
test("Array with number should return that number", () => {
expect(findMax([10])).toEqual(10);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test("Negative and positive numbers should return the largest overall", () => {
expect(findMax([10, -5, 58, -56])).toEqual(58);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("Negative numbers should return the closest to zero", () => {
expect(findMax([-5, -10, -65, -6])).toEqual(-5);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("Decimal numbers should return the largest one", () => {
expect(findMax([0.5, 10.5, 56.9, 3.5])).toEqual(56.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("Non-numeric values should be ignored and return the max numeric", () => {
expect(findMax(["hey", 10, "hi", 60, 10])).toEqual(60);
});

// 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("Only non-number values should return undefined", () => {
expect(findMax(["hey", "love", "hi", "hate"])).toEqual("undefined");
});