find the volume of a sphere in typescript

Here's a code snippet that calculates the volume of a sphere in TypeScript:

function calculateSphereVolume(radius: number): number {
  const volume = (4/3) * Math.PI * Math.pow(radius, 3);
  return volume;

const radius = 5;
const volume = calculateSphereVolume(radius);

console.log(`The volume of the sphere with radius ${radius} is ${volume}`);
274 chars
10 lines

In this code, we defined a function called calculateSphereVolume that takes the radius of the sphere as a parameter and returns the volume of the sphere. We then call this function with a radius of 5 and log the result to the console.

The formula for calculating the volume of a sphere is V = (4/3) * π * r^3, where r is the radius of the sphere. We use this formula in our TypeScript function to calculate the volume of the sphere.

The Math.PI property represents the value of pi, and Math.pow() is used to raise the radius to the power of 3. The volume is then returned by the function.

gistlibby LogSnag