find the surface area of a rectangular prism in typescript

To find the surface area of a rectangular prism, we need to calculate the area of each face and then add them up. Here's how to do it in typescript:

index.ts
function surfaceAreaRectangularPrism(length: number, width: number, height: number): number {
    const baseArea = length * width;
    const sideArea = width * height;
    const frontArea = length * height;
  
    return 2 * baseArea + 2 * sideArea + 2 * frontArea;
}
268 chars
8 lines

In this code, we define a function surfaceAreaRectangularPrism that takes in the length, width, and height of the rectangular prism as parameters. Inside the function, we calculate the area of the base, side, and front faces of the prism using the length, width, and height parameters. Finally, we add up the areas of all six faces (since a rectangular prism has six faces) and return the total surface area.

You can call this function with the dimensions of your rectangular prism, like this:

index.ts
const length = 5;
const width = 3;
const height = 2;

const surfaceArea = surfaceAreaRectangularPrism(length, width, height);
console.log(`The surface area of the rectangular prism is ${surfaceArea}`);
202 chars
7 lines

This will output:

index.ts
The surface area of the rectangular prism is 62
48 chars
2 lines

gistlibby LogSnag