find the area of a triangle in typescript

Here's how to calculate the area of a triangle in TypeScript:

index.ts
function calculateTriangleArea(base: number, height: number): number {
  return (base * height) / 2;
}

// Example usage
const base = 10;
const height = 5;

const area = calculateTriangleArea(base, height);
console.log(`The area of the triangle is ${area}.`);
260 chars
11 lines

Explanation:

  • We define a function called calculateTriangleArea that takes two parameters; base and height, both of type number.
  • The formula to find the area of a triangle is base x height / 2. We apply this formula inside the function and return the result.
  • In the example usage, we define the value of base and height and call the calculateTriangleArea function. The result is stored in the variable area and then logged to the console.

gistlibby LogSnag