HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Calculate the nth root of a number in JavaScript

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptnthcalculatetherootnumber

Problem

The nth root of a number x is a value that, when multiplied by itself n times, gives x. The nth root can also be expressed as a power of x, where x ^ (1/n) is equal to the nth root of x.
Given this, we can use Math.pow() to calculate the nth root of a given number. Simply pass it the number x and a power of 1 / n, and you'll get the nth root of x.

Solution

Implementation for Calculate the nth root of a number in JavaScript:

const nthRoot = (x, n) => Math.pow(x, 1 / n);

nthRoot(32, 5); // 2


See the code snippet for the complete implementation.

Code Snippets

const nthRoot = (x, n) => Math.pow(x, 1 / n);

nthRoot(32, 5); // 2

Context

From 30-seconds-of-code: nth-number-root

Revisions (0)

No revisions yet.