find the volume of a cone in javascript

To find the volume of a cone in JavaScript, we need to use the formula:

V = (1/3) * Math.PI * r^2 * h

Where:

  • V is the volume of the cone
  • r is the radius of the base of the cone
  • h is the height of the cone

Here is the JavaScript code to calculate the volume of a cone given the radius and height:

index.tsx
function coneVolume(radius, height) {
  const volume = (1/3) * Math.PI * Math.pow(radius, 2) * height;
  return volume;
}

// Example usage:
const coneRadius = 5;
const coneHeight = 10;
const coneVol = coneVolume(coneRadius, coneHeight);

console.log(`The volume of the cone with radius ${coneRadius} and height ${coneHeight} is ${coneVol.toFixed(2)} cubic units.`);
367 chars
12 lines

Output:

index.tsx
The volume of the cone with radius 5 and height 10 is 261.80 cubic units.
74 chars
2 lines

gistlibby LogSnag