숫자의 끝 근을 계산하려면 Math.abs() 메서드를 사용하십시오. 여기에서도 Match.pow 메서드를 사용하고 있습니다. JavaScript에서 숫자의 제곱근을 계산하기 위해 다음 코드를 실행할 수 있습니다 -
예시
<html>
<head>
<title>Calculate root</title>
</head>
<body>
<script>
function displayRoot(x, num) {
val = num % 2;
if((val == 1) || x < 0)
x = -x;
var a, num;
a = Math.pow(x, 1 / num);
num = Math.pow(a, num);
if(Math.abs(x - num) < 1 && (x > 0 === num > 0))
return val ? -a : a;
}
document.write(displayRoot(36, 2));
document.write("<br>"+displayRoot(49, 2));
document.write("<br>"+displayRoot(36, -2));
document.write("<br>"+displayRoot(216, -2));
</script>
-+
</body>
</html>