हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो किसी संख्या के nवें रूट की गणना करता है और उसे वापस करता है।
उदाहरण
इसके लिए कोड होगा -
const findNthRoot = (m, n) => {
try {
let negate = n % 2 == 1 && m < 0;
if(negate)
m = −m;
let possible = Math.pow(m, 1 / n);
n = Math.pow(possible, n);
if(Math.abs(m − n) < 1 && (m > 0 == n > 0))
return negate ? −possible : possible;
} catch(e){
return null;
}
};
console.log(findNthRoot(45, 6)); आउटपुट
और कंसोल में आउटपुट होगा -
1.8859727740585395