计算JavaScript中直角三角形的斜边
我们需要编写一个包含两个数字的JavaScript函数。第一个数字代表直角三角形底边的长度,第二个数字垂直。然后,该函数应根据这些值计算斜边的长度。
例如-
如果基=8,垂直=6
然后输出应该是10
示例
以下是代码-
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));输出结果
以下是控制台上的输出-
10 65.5133574166368