Upper right triangle
The pattern of this triangle contains the maximum number of start at the right side and the bottom. It is another type of right-angled triangle.
Example 2: The below example explains the logic to print the upper right triangle.
let n = 5;
for (let i = 1; i <= n; i++) {
let str = "* ";
let space = ' ';
console.log(space.repeat((n-i))+str.repeat(i));
}
Output
* * * * * * * * * * * * * * *
JavaScript Program to Print Triangle Star Pattern
This article will show you how to make a triangle star pattern using JavaScript. We’ll use a for loop to iterate through the number of rows, and in each iteration we will use a for loop for the n number of iterations, and in each loop we will print the “*” with the repeat method to increase or decrease the number of stars in each iteration. Additionally, we’ll create a whitespace string to add the necessary spaces before the stars to ensure proper alignment. Below are the star patterns we’ll cover:
Table of Content
- Upper left triangle
- Upper right triangle
- Lower Right Triangle
- Lower Left Triangle