Calculate the hypotenuse of a right triangle in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in two numbers. The first number represents the length of the base of a right triangle and the second is perpendicular. The function should then compute the length of the hypotenuse based on these values.

For example −

If base = 8, perpendicular = 6

Then the output should be 10

Example

Following is the code −

const base = 8;
const perpendicular = 6;
const findHypotenuse = (base, perpendicular) => {
   const bSquare = base ** 2;
   const pSquare = perpendicular ** 2;
   const sum = bSquare + pSquare;
   const hypotenuse = Math.sqrt(sum);
   return hypotenuse;
};
console.log(findHypotenuse(base, perpendicular));
console.log(findHypotenuse(34, 56));

Output

Following is the output on console −

10
65.5133574166368
raja
Published on 11-Dec-2020 14:10:07
Advertisements