find the volume of a sphere in javascript

To find the volume of a sphere in JavaScript, you can use the formula:

index.tsx
V = (4/3) * Math.PI * Math.pow(r, 3)
37 chars
2 lines

Where r is the radius of the sphere.

Here's an example function that takes the radius as an argument and returns the volume of the sphere:

index.tsx
function sphereVolume(radius) {
  const volume = (4/3) * Math.PI * Math.pow(radius, 3);
  return volume.toFixed(2); // Round the result to 2 decimal places
}

// Example usage
const radius = 5;
const volume = sphereVolume(radius);
console.log(`The volume of a sphere with radius ${radius} is ${volume}`);
305 chars
10 lines

Output:

index.tsx
The volume of a sphere with radius 5 is 523.60
47 chars
2 lines

gistlibby LogSnag