- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate the hypotenuse of a right triangle in JavaScript
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
Advertisements
To Continue Learning Please Login
Login with Google