![]() |
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 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: Table of Content Using FunctionCalculate 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.
Output 270 Time Complexity: O(1). Space Complexity: O(1). Using ClassDefine 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.
Output Surface Area is: 270 Time Complexity: O(1). Space Complexity: O(1). |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |