find the nth root of a number in typescript

To find the nth root of a number in TypeScript, you can use the Math.pow function, which takes two arguments: the base and the exponent, and returns the result of raising the base to the given exponent. The nth root of a number can be found by raising the number to the power of 1/n. Here's an example TypeScript function that finds the nth root of a number:

index.ts
function nthRoot(num: number, n: number): number {
  return Math.pow(num, 1/n);
}
82 chars
4 lines

You can then call this function with the number and the root you want to find, like this:

index.ts
const num = 125;
const nth = 3;
const result = nthRoot(num, nth); // result = 5
80 chars
4 lines

In this example, we're finding the 3rd root of the number 125, which is 5.

related categories

gistlibby LogSnag