find the area of a trapezoid in typescript

To find the area of a trapezoid in TypeScript, we need to know the length of both bases and the height of the trapezoid. We can use the following formula to calculate the area:

index.ts
Area = (Base1 + Base2) * Height / 2
36 chars
2 lines

Here is an implementation of the formula in TypeScript:

index.ts
function getTrapezoidArea(base1: number, base2: number, height: number): number {
    return (base1 + base2) * height / 2;
}

// Example usage
const area = getTrapezoidArea(5, 10, 7);
console.log(area); // Output: 45
217 chars
8 lines

In this example, we define a getTrapezoidArea() function that takes in the length of the two bases and the height of the trapezoid as parameters. The function returns the area of the trapezoid using the aforementioned formula.

We can then call the function with sample values of base1, base2, and height and log the result to the console. The output would be 45, which is the area of the trapezoid.

gistlibby LogSnag