Tuesday, February 2, 2016

Javiscript deterimine square root code

var x=16;
var n=2;
    var negate = n % 2 == 1 && x < 0;
    if(negate)
      x = -x;
    var possible = Math.pow(x, 1 / n);
    n = Math.pow(possible, n);
    if(Math.abs(x - n) < 1 && (x > 0 == n > 0))
      var result= negate ? -possible : possible;
 
document.getElementById("demo").innerHTML =
"
" + result;