find the cube root of a number in typescript

To find the cube root of a number in TypeScript, we can write a function using the Math module. Here's an example:

index.ts
function cubeRoot(num: number): number {
  return Math.cbrt(num);
}
68 chars
4 lines

In this function, num is the number whose cube root we want to find. The Math.cbrt() method returns the cube root of a number. We simply call this method with num as the argument, and return the result.

We can then call this cubeRoot() function with any number argument, like this:

index.ts
console.log(cubeRoot(27)); // Output: 3
console.log(cubeRoot(64)); // Output: 4
console.log(cubeRoot(125)); // Output: 5
121 chars
4 lines

In this example, we pass in the numbers 27, 64, and 125, and the function returns their respective cube roots: 3, 4, and 5.

gistlibby LogSnag