How to calculate the nth root of a number in JavaScript?


We have given a number and the task is to calculate the nth root of that number using JavaScript. We have multiple ways to achieve this, some of which are the following.

  • Using Math.pow() Method

  • Using Logarithm

To get the nth root of a number firstly let’s understand whose root we can calculate and which number’s root we cannot calculate.

  • If the number is positive and the root is also even then we will get two solutions. E.g − 2nd root of 16 is +4 and -4, similarly 4th root of 16 is +2 and -2

  • If the number is negative and the root is odd then we will get one negative solution. E.g − 3rd root of -27 is only -3 (Not 3).

  • If the number is positive and the root is odd then we will get one positive solution. E.g − 3rd root of 27 is 3

  • If the number is negative and the root is even the there is no solution.

Using the Math.pow() Method

The Math.pow() function in JavaScript is used to get the power of a number. The value of number raised to some exponent. This method accepts two parameters one is base and the other is exponent. It doesn't matter what kind of parameters do we use such as integers, fractions, negatives etc., it can take any value and proceeds the operation.

Syntax

Math.pow( X, Y )

Parameters

  • X − This is the base of which power we want.

  • Y − This is the exponent

Steps

You can follow the below given steps to find the nth power of a number using the Math.power() method

STEP 1 − Check whether the Base is positive or negative. And the nth-root is odd or even.

STEP 2 − If the base is positive and the root is even. The answer will be the positive and negative version of the result.

STEP 3 − If the base is positive and the root is odd. The answer will be the positive version of the result.

STEP 4 − If the base is negative and the root is even then the answer will be the negative version of the result.

STEP 5 − If the base is negative and the root is odd then there is No-Answer.

Note − In Math.pow() we will always pass absolute values of the base.

Example

In this example, we calculate the nth root of various numbers.

<html lang="en">
   <body>
      <h2> Calculating the nth root of a number in JavaScript </h2>
      <p>Here are the nth root of the following values</p>
      <p id="p1"></p> <p id="p2"></p> <p id="p3"></p>
      <script>
         calcRoot(16, 2, "p1");
         calcRoot(27, 3, "p2");
         calcRoot(-27, 3, "p3");
         function calcRoot(base, root, id){
         let isBasePositive = base > 0 ? true : false;
         let isRootEven = root % 2 == 0 ? true : false;
         let p = document.getElementById(id);
         if(base == 0){
            p.innerText = `${root}th root of ${base} is 0`
            return
         }
         if(root == 0){
            p.innerText = `${root}th root of ${base} is Infinity`
            return
         }
         if(isBasePositive && isRootEven){
            let ans = Math.pow(base, 1 / root);
            p.innerText = `${root}th root of ${base} is ${ans} and -${ans}`
         }
         if(isBasePositive && !isRootEven){
            let ans = Math.pow(base, 1 / root);
            p.innerText = `${root}th root of ${base} is ${ans}`
         }
         if(!isBasePositive && !isRootEven){
            let ans = Math.pow( Math.abs( base ), 1 / root);
            p.innerText = `${root}th root of ${base} is -${ans}`
         }
         if(!isBasePositive && isRootEven){
               p.innerText = `${root}th root of ${base} is NaN`
            }
         }
      </script>
   </body>
</html>

Using Logarithm

Example

In this method, we use the logarithm to calculate the nth root. Now, converting this statement in JavaScript expression −

R = Math.exp(1 / root * Math.log(base))

In this example we are calculating roots of multiple numbers 

<html>
   <body>
      <h2> Calculating the nth root of a number in JavaScript </h2>
      <p>Here are the nth root of the following values</p>
      <p id="p1"></p>
      <p id="p2"></p>
      <script>
         calcRoot(16, 2, "p1")
         calcRoot(49, 2, "p2")
         function calcRoot(base, root, id){
            let r = Math.exp(1 / root * Math.log(base))
            let p = document.getElementById(id);
            p.innerText = `${root}th root of ${base} is ${r}`
         }
      </script>
   </body>
</html>

In summary, there are multiple ways to calculate the nth root of a number in JavaScript. One method is to use the Math.pow() function, which takes in two parameters: a base and an exponent. The base is the number for which you want to calculate the root, and the exponent is the root you want to calculate. To get the nth root of a number using Math.pow(), you can use the following formula: Math.pow(base, 1/root).

You can also use the logarithm method, which involves using the Math.log() function to calculate the root. To do this, you can use the following formula: Math.exp(Math.log(base)/root). It is important to consider whether the base is positive or negative and whether the root is even or odd, as this will affect the possible solutions.

Updated on: 06-Jan-2023

895 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements