You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
programming-examples/js/Math/Calculate the nth root of a...

13 lines
372 B
JavaScript

function nthroot(x, n)
{
ng = n % 2;
if((ng == 1) || x<0)
x = -x;
var r = Math.pow(x, 1 / n);
n = Math.pow(r, n);
if(Math.abs(x - n) < 1 && (x > 0 === n > 0))
return ng ? -r : r;
}
console.log(nthroot(64, 2));
console.log(nthroot(64, -2));