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 :

Sample Solution:-

HTML Code:

<!DOCTYPE html>
<meta charset="utf-8">
<title>Pythagorean Theorem</title>

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:



Flowchart: JavaScript Math- Pythagorean function in JavaScript

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.

Follow us on Facebook and Twitter for latest update.