JavaScript : Pythagorean function in JavaScript
JavaScript Math: Exercise-19 with Solution
Create a Pythagorean function in JavaScript.
Note : The Pythagorean Theorem tells us that the relationship in every right triangle is : c2 = a2 + b2, where c is the hypotenuse and a, b are two legs of the triangle.
Test Data:
console.log(pythagorean_theorem(2, 4));
console.log(pythagorean_theorem(3, 4));
Output :
4.47213595499958
5
Sample Solution:-
HTML Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Pythagorean Theorem</title>
</head>
<body></body>
</html>
JavaScript Code:
function pythagorean_theorem(x, y) {
if ((typeof x !== 'number') || (typeof y !== 'number'))
return false;
return Math.sqrt(x * x + y * y);
}
console.log(pythagorean_theorem(2, 4));
console.log(pythagorean_theorem(3, 4));
Sample Output:
4.47213595499958 5
Flowchart:

Live Demo:
See the Pen javascript-math-exercise-19 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus
Previous: Write a JavaScript function to calculate the product of values in an array.
Next: Write a JavaScript program to evaluate binomial coefficients.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join