
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate the Nth Root of a Number in JavaScript
We are required to write a JavaScript function that calculates the nth root of a number and returns it.
Example
The code for this will be −
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));
Output
And the output in the console will be −
1.8859727740585395
Advertisements