JavaScript Program to Calculate the Surface Area of a Triangular Prism
Last Updated :
20 May, 2024
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 PrismExample:
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));
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);
OutputSurface Area is: 270
Time Complexity: O(1).
Space Complexity: O(1).
Similar Reads
JavaScript Program to Find Surface Area of a Cone Mathematically, the Surface area of the Cone can be calculated using the formula: area = pi * r * s + pi * r^2, Where r is the radius of the circular base, and s is the slant height of the cone. We will calculate the Surface Area of a Cone programmatically using JavaScript. These are the following m
2 min read
JavaScript Program to Find Curved Surface Area of a Cone A cone is a three-dimensional geometric shape. It consists of a base having the shape of a circle and a curved side (the lateral surface) ending up in a tip called the apex or vertex. We will be going to learn how can we calculate it by using JavaScript. Formula used:Curved Surface area of a Cone =
2 min read
JavaScript program to find perimeter of a triangle Given the side (a, b, c) of a triangle and we have to find the perimeter of a triangle. Perimeter: Perimeter of a triangle is the sum of the length of the side of a triangle. Where a, b, and c are lengths of sides of a triangle. The perimeter of a triangle can be simply evaluated using the following
1 min read
JavaScript Program to Find the Area of Trapezium A Trapezium or (Trapezoid) is a convex quadrilateral with at least one pair of parallel sides. The parallel sides are called the bases of the trapezoid and the other two sides which are not parallel are referred to as the legs. There can also be two pairs of bases. The area of a trapezium can be fou
2 min read
JavaScript Program to Find Volume of Right Wedge A wedge is a three-dimensional object with two triangular faces and one rectangular face. Examples: Input: a = 2, b = 5, e = 5, h = 6 Output: Volume = 45.0Input: a = 5, b = 4, e = 4, h = 6 Output: Volume = 56.0 Approach: Va is the volume of triangular pyramid i.e. Va = (1 / 3) * area of triangle *
3 min read