DEV Community

Cover image for Determine the Maximum Number
Jackson Reeves
Jackson Reeves

Posted on

Determine the Maximum Number

I overshot the goal on a homework assignment and came up with a function that would handle edge cases. It was just supposed to take in three distinct numerical inputs and determine which one was the largest. But I built my function to account for the possibility that two or even all three of the numbers might be identical, plus the possibility that one or all of the inputs might be non-numerical. It may have been overkill, but I’m pretty proud of how much I nerded out here.

function maxOfThree(num1, num2, num3) {
if (num1 > num2 && num1 > num3) {
return num1;
} else if (num2 > num1 && num2 > num3) {
return num2;
} else if (num3 > num1 && num3 > num2) {
return num3;
} else if (num1 === num2 === num3) {
return "These numbers are identical.";
} else if (num1 === num2 || num2 === num3 || num1 === num3) {
// In case two numbers were the same:
if (num1 > num2) {
return num1;
} else if (num2 > num3) {
return num2;
} else if (num3 > num1) {
return num3;
} else {
// In case the input were not numbers:
return undefined;
}
}
view raw max-of-three.js hosted with ❤ by GitHub

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs