Member-only story

JavaScript Algorithm: Maximum Edge of a Triangle

(Beginner) No more geometry headaches with this handy coding solution

Max N
1 min readFeb 29, 2024

When working with triangles in programming, it’s often handy to quickly know the possible range of their third unseen edge. But trying to recall all those geometry rules gives most developers a headache pretty fast.

Luckily, we can neatly find a triangle’s maximum third edge range with JavaScript with no formulas required!

In this quick article, we’ll write a function accepting two triangle edges as inputs and returning the highest possible value for determining that mystery third side.

Defining Our Function

First, let’s outline the signature and parameters:

function getMaxThirdEdge(side1, side2) {

}

By defining side1 and side2 here, we tell our function to accept those triangle edge values.

Calculating the Range

Inside, we simply add the two known sides together and subtract 1:

function getMaxThirdEdge(side1, side2) {
var maxLength = side1 + side2 - 1;

}

Returning the Result

Finally, we return the maximum possible length:

function getMaxThirdEdge(side1, side2) {
var maxLength = side1 + side2 - 1;

}

Let’s See It In Action

Calling our method with different lengths shows it works perfectly:

getMaxThirdEdge(3, 4); // 6
getMaxThirdEdge(5, 7); // 11

Now you can instantly find triangle third edge maximums.

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet