Horje
JavaScript Program to Calculate the Surface Area of a Triangular Prism

Given base, height, length, side1, side2, and side3, our task is to calculate the surface area of a triangular prism in JavaScript. A triangular prism is a 3D geometric shape with two triangular bases and three rectangular faces. These rectangular faces connect the corresponding sides of the two triangular bases.

triangular-priosm

Triangular Prism

Example:

Input:
base = 5; height = 6; length = 10; side1 = 8; side2 = 9; side3 = 7;

Output:
270

Explanation:
The Formula of Triangular Prism is: (b ∗ h)+(L ∗ (s1 + s2 +s3))
where, 
b= base of the triangle
h= height of the triangle
L = Length of the prism
s1= side 1 of the triangle face
s2= side 2 of the triangle face
s3= side 3 of the triangle face

Below are the following approaches for calculating the Surface Area of a Triangular Prism using JavaScript:

Using Function

Calculate the area of the triangular bases by multiplying the base and height of the triangular base by 2. Calculate the perimeter of the triangular base by summing the lengths of its sides. Calculate the area of the rectangular faces by multiplying the length of the prism by the perimeter of the triangular base. Add the areas of the triangular bases and rectangular faces to get the total surface area of the triangular prism. Return the total surface area.

Example: The example below shows how to calculate the Surface Area of a Triangular Prism using Function.

JavaScript
function surfArea(base, height, len, s1, s2, s3) {
    // Area of triangular bases
    const areaBase = 0.5 * base * height;

    // Areas of rectangular faces
    const areaRect1 = s1 * len;
    const areaRect2 = s2 * len;
    const areaRect3 = s3 * len;

    // Total surface area
    const totalArea = 2 * areaBase + areaRect1 + areaRect2 + areaRect3;

    // Return total surface area
    return totalArea;
}

// Triangular base dimensions
const base = 5;
const height = 6;

// Prism length
const len = 10;

// Sides of the triangular base
const s1 = 8;
const s2 = 9;
const s3 = 7;

console.log(surfArea(base, height, len, s1, s2, s3)); 

Output
270

Time Complexity: O(1).

Space Complexity: O(1).

Using Class

Define a class TriangularPrism with a constructor that initializes the properties base, height, length, side1, side2, and side3. Now define a method calSurfaceArea() within the class to compute the surface area using the provided formula. We create an instance of the TriangularPrism class with the given dimensions and then call the calSurfaceArea() method to get the surface area. Return the total surface area.

Example: The example below shows how to calculate the Surface Area of a Triangular Prism using class.

JavaScript
class TriPrism {
    constructor(base, height, len, s1, s2, s3) {
        this.base = base;
        this.height = height;
        this.len = len;
        this.s1 = s1;
        this.s2 = s2;
        this.s3 = s3;
    }

    surfArea() {
        // Area of the two triangular bases
        const triArea = 0.5 * this.base * this.height * 2;

        // Area of the three rectangular faces
        const rectArea = (this.s1 + this.s2 + this.s3) * this.len;

        // Total surface area
        return triArea + rectArea;
    }
}

const prism = new TriPrism(5, 6, 10, 8, 9, 7);
const surfaceArea = prism.surfArea();
console.log("Surface Area is:", surfaceArea);

Output
Surface Area is: 270

Time Complexity: O(1).

Space Complexity: O(1).




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Disable a Button Conditionally in JavaScript? How to Disable a Button Conditionally in JavaScript?
Nesting For Loops in JavaScript Nesting For Loops in JavaScript
How to Create a Filter List using JavaScript? How to Create a Filter List using JavaScript?
JavaScript Program to Check if a Binary Tree is Complete JavaScript Program to Check if a Binary Tree is Complete
JavaScript Program to Find the Rank of a Matrix JavaScript Program to Find the Rank of a Matrix

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16