Member-only story

JavaScript Algorithm: Maximum Edge of a Triangle

Finding the maximum possible value of a triangle’s third side

Max N
2 min readJul 3, 2022
Photo by Pawel Czerwinski on Unsplash

Today, we write a function called nextSide that will accept two arguments, side1 and side2, as integers.

You are given the lengths of two sides of a triangle. Assuming that the side lengths are all positive integers, the goal of the function is to output the maximum possible length of the third side.

This is the formula to get the maximum value of the third side:

(side1 + side2) - 1

Here are some examples:

nextSide(8, 10) // 17

nextSide(5, 7) // 11

nextSide(9, 2) // 10

Using the examples, we get our result by plugging the side lengths of the triangles into our formula above:

(8 + 10) - 1 = 17
(5 + 7) - 1 = 11
(9 + 2) - 1 = 10

To write our function, all we have to do is return the result of the formula:

return (side1 + side2) - 1

Here is the rest of the code:

function nextSide(side1, side2){
return (side1 + side2) - 1;
}

If you find this algorithm helpful, check out my other JavaScript algorithm solution articles:

--

--

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