find the surface area of a triangular prism in typescript

To find the surface area of a triangular prism in TypeScript, you would need to use the formula:

Surface Area = (2 x Base Area) + (Perimeter of Base x Height of Prism)

Where Base Area is the area of the triangular base and Perimeter of Base is the perimeter of the triangular base. To calculate these values, we need to find the length of the sides of the triangle by using the Pythagorean theorem.

Here's the code example:

index.ts
class TriangularPrism {
  base: number;
  height: number;
  prismHeight: number;

  constructor(base: number, height: number, prismHeight: number) {
    this.base = base;
    this.height = height;
    this.prismHeight = prismHeight;
  }

  get surfaceArea(): number {
    const baseArea = 0.5 * this.base * this.height;
    const perimeterOfBase = 3 * this.base;
    const lateralSurfaceArea = perimeterOfBase * this.prismHeight;
    const totalSurfaceArea = 2 * baseArea + lateralSurfaceArea;

    return totalSurfaceArea;
  }
}

// Example usage
const triangularPrism = new TriangularPrism(4, 3, 5);
console.log(triangularPrism.surfaceArea); // Output: 62
658 chars
25 lines

In this code, we create a class TriangularPrism with properties base, height, and prismHeight. We then define a getter surfaceArea that calculates the surface area of the triangular prism using the formula mentioned earlier.

To find the Base Area we calculate the area of the base of the triangular prism (1/2 bh). To find the Perimeter of Base we calculate the perimeter of the base of the triangular prism (3 x base) and then multiply that by the height of the prism to get the lateral surface area Finally we sum the area of the two bases 2*baseArea and the lateral surface area to get the final surface area.

We instantiate an object of TriangularPrism with base of 4, height of 3, and prism height of 5. We then call the surfaceArea getter to get the surface area of the prism which will output 62.

gistlibby LogSnag