JavaScript hypot

JavaScript 的 hypot 函数是 Math 函数之一,用于返回指定参数平方和的平方根。hypot 函数的语法是:

Math.hypot(Value1, Value2, ...ValueN);

JavaScript hypot 函数示例

在此 JS hypot 函数示例中,我们将找到不同数据类型的平方根并显示输出。首先,我们直接对正整数和负整数使用了 hypot 函数。

√2² + 3² = √13 = 3.605

接下来,我们将此 Math 函数应用于字符串值。它会将“3”转换为整数。对于 Str1,我们尝试了字符串值“String”。JavaScript 将返回 NaN(非数字)作为输出。然后,我们使用 Null 值。这里的 null 参数被转换为零。

<!DOCTYPE html>
<html>
<head>
    <title> JavaScriptHYPOTFunction </title>
</head>
<body>
  <h1> JavaScriptHYPOTFunction </h1>
  <p id = "Pos"></p>
  <p id = "Neg"></p>
  <p id = "Dec"></p>
  <p id = "Neg_Dec"></p>
  <p id = "Str"></p>
  <p id = "Str1"></p>
  <p id = "Null"></p>
  <p id = "Multi"></p>
  <script>
    document.getElementById("Pos").innerHTML = Math.hypot(2, 3);
    document.getElementById("Neg").innerHTML = Math.hypot(2, -4);
    document.getElementById("Dec").innerHTML = Math.hypot(2.50, 4.05);
    document.getElementById("Neg_Dec").innerHTML = Math.hypot(-3.10,-6.05);
    document.getElementById("Str").innerHTML = Math.hypot(2, "3");
    document.getElementById("Str1").innerHTML = Math.hypot(2, "String");
    document.getElementById("Null").innerHTML = Math.hypot(4, null);
    document.getElementById("Multi").innerHTML = Math.hypot(2, 3, 4);
  </script>
</body>
</html>
HYPOT Function Example