find the surface area of a pyramid in typescript

To find the surface area of a pyramid in TypeScript, you'll need to have the length of the base edges and the slant height of the pyramid. Here's the TypeScript code to calculate the surface area:

index.ts
class Pyramid {
  baseEdgeLength: number;
  slantHeight: number;

  constructor(b: number, h: number) {
    this.baseEdgeLength = b;
    this.slantHeight = h;
  }

  // calculate the surface area of the pyramid
  getSurfaceArea(): number {
    const baseArea = Math.pow(this.baseEdgeLength, 2);
    const perimeter = this.baseEdgeLength * 4;
    const triangleArea = (perimeter * this.slantHeight) / 2;
    return baseArea + triangleArea;
  }
}

// usage example
const pyramid = new Pyramid(5, 8);
console.log(pyramid.getSurfaceArea()); // expected output: 65.33333333333333
575 chars
22 lines

In the above code, we have defined a Pyramid class with two properties - baseEdgeLength and slantHeight. We have also defined a constructor to initialize these properties.

getSurfaceArea() is a method of the Pyramid class that calculates and returns the surface area of the pyramid using the formula Base Area + (Perimeter × Slant Height) / 2.

We have created an instance of the Pyramid class, passed in the baseEdgeLength and slantHeight as arguments, and called the getSurfaceArea() method to get the surface area of the pyramid.

The expected output of the code snippet is 65.33333333333333.

gistlibby LogSnag