find the surface area of a sphere in typescript

To find the surface area of a sphere in TypeScript, you can use the following formula:

index.ts
surfaceArea = 4 * pi * radius^2
32 chars
2 lines

Where pi is a constant value approximately equal to 3.14159, and radius is the distance from the center of the sphere to any point on its surface.

Here's an example function that calculates the surface area of a sphere in TypeScript:

index.ts
function sphereSurfaceArea(radius: number): number {
  const pi = 3.14159;
  const surfaceArea = 4 * pi * Math.pow(radius, 2);

  return surfaceArea;
}
152 chars
7 lines

You can call this function and pass in the radius of your sphere as an argument:

index.ts
const radius = 5;
const area = sphereSurfaceArea(radius);

console.log(`The surface area of a sphere with radius ${radius} is ${area}.`);
138 chars
5 lines

This will output:

index.ts
The surface area of a sphere with radius 5 is 314.159.
55 chars
2 lines

gistlibby LogSnag