0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Print Right Triangle Pattern

Use nested for loops in JavaScript where the outer loop controls the rows and the inner loop prints stars * to form a right triangle pattern, like for(let i=1; i<=n; i++){ let row = ''; for(let j=1; j<=i; j++){ row += '*'; } console.log(row); }.
📋

Examples

Input3
Output* ** ***
Input5
Output* ** *** **** *****
Input1
Output*
🧠

How to Think About It

To print a right triangle pattern, think of each row as having a number of stars equal to the row number. Start from 1 star in the first row, then 2 stars in the second, and so on until the given number of rows. Use one loop to go through each row and another loop inside it to print the stars for that row.
📐

Algorithm

1
Get the number of rows (n) as input.
2
For each row from 1 to n:
3
Initialize an empty string for the current row.
4
Add stars (*) to the string equal to the current row number.
5
Print the string for the current row.
💻

Code

javascript
const n = 5;
for (let i = 1; i <= n; i++) {
  let row = '';
  for (let j = 1; j <= i; j++) {
    row += '*';
  }
  console.log(row);
}
Output
* ** *** **** *****
🔍

Dry Run

Let's trace the code for n = 3 to see how the right triangle pattern is printed.

1

Start outer loop with i = 1

Initialize row = ''; inner loop runs j from 1 to 1; row becomes '*'; print '*'

2

Outer loop i = 2

Initialize row = ''; inner loop runs j from 1 to 2; row becomes '**'; print '**'

3

Outer loop i = 3

Initialize row = ''; inner loop runs j from 1 to 3; row becomes '***'; print '***'

i (row)j (stars)row string
11*
21,2**
31,2,3***
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to n, representing each row of the triangle.

Step 2: Inner loop prints stars

The inner for loop runs from 1 to the current row number, adding stars to the row string.

Step 3: Print each row

After building the row string with stars, console.log prints the row, forming the triangle shape.

🔄

Alternative Approaches

Using string repeat method
javascript
const n = 5;
for (let i = 1; i <= n; i++) {
  console.log('*'.repeat(i));
}
This method is shorter and uses built-in string repetition, improving readability.
Using while loops
javascript
let i = 1;
const n = 5;
while (i <= n) {
  let j = 1;
  let row = '';
  while (j <= i) {
    row += '*';
    j++;
  }
  console.log(row);
  i++;
}
Uses while loops instead of for loops, which some beginners find easier to understand.

Complexity: O(n^2) time, O(n) space

Time Complexity

The outer loop runs n times and the inner loop runs up to n times in the last iteration, resulting in O(n^2) time.

Space Complexity

The space used is O(n) for the string that holds stars for the largest row.

Which Approach is Fastest?

Using the string repeat() method is faster and cleaner than nested loops because it avoids manual concatenation.

ApproachTimeSpaceBest For
Nested loopsO(n^2)O(n)Understanding loops and manual string building
String repeat methodO(n^2)O(n)Cleaner and faster star printing
While loopsO(n^2)O(n)Beginners preferring while loops
💡
Use the string repeat() method to print stars quickly without an inner loop.
⚠️
A common mistake is printing all stars in one line instead of printing a new line for each row.