find the surface area of a cylinder in javascript

To find the surface area of a cylinder in JavaScript, we need to know the radius and height of the cylinder. The formula to calculate the surface area of a cylinder is:

index.tsx
SA = 2πr(r + h)
16 chars
2 lines

Where:

  • SA is the surface area
  • π is the mathematical constant pi (approximately equal to 3.14)
  • r is the radius of the base of the cylinder
  • h is the height of the cylinder

Here's the JavaScript code to find the surface area of a cylinder:

index.tsx
function surfaceAreaOfCylinder(radius, height) {
  const pi = 3.14;
  const sa = 2 * pi * radius * (radius + height);
  return sa;
}

//Example
console.log(surfaceAreaOfCylinder(5, 8)); // Returns: 329.2 (units squared)
220 chars
9 lines

In this code, we define a function surfaceAreaOfCylinder that takes the radius and height of the cylinder as parameters. We then define the constant pi as 3.14 and use the formula to calculate the surface area of the cylinder. Finally, the function returns the surface area.

In the example provided, calling the surfaceAreaOfCylinder function with a radius of 5 and a height of 8 would return 329.2 (units squared), which is the surface area of the cylinder.

gistlibby LogSnag