JavaScript Program to Print Pyramid Pattern
Use nested
for loops in JavaScript to print a pyramid pattern by printing spaces and stars in each line, like for(let i=1; i<=n; i++){ let row = ' '.repeat(n-i) + '* '.repeat(i); console.log(row); }.Examples
Input3
Output *
* *
* * *
Input5
Output *
* *
* * *
* * * *
* * * * *
Input1
Output*
How to Think About It
To print a pyramid pattern, think of each line as having some spaces followed by stars. The number of spaces decreases each line, and the number of stars increases. Use one loop to go through each line and inside it, print spaces first, then stars.
Algorithm
1
Get the number of rows (n) for the pyramid.2
For each line from 1 to n:3
Calculate and print (n - current line) spaces.4
Print stars equal to the current line number, each followed by a space.5
Move to the next line and repeat until all lines are printed.Code
javascript
const n = 5; for (let i = 1; i <= n; i++) { let row = ' '.repeat(n - i) + '* '.repeat(i); console.log(row); }
Output
*
* *
* * *
* * * *
* * * * *
Dry Run
Let's trace the pyramid printing for n=3 through the code
1
First line (i=1)
Print 2 spaces and 1 star: ' * '
2
Second line (i=2)
Print 1 space and 2 stars: ' * * '
3
Third line (i=3)
Print 0 spaces and 3 stars: '* * * '
| i | Spaces | Stars | Row Output |
|---|---|---|---|
| 1 | 2 | 1 | * |
| 2 | 1 | 2 | * * |
| 3 | 0 | 3 | * * * |
Why This Works
Step 1: Spaces decrease each line
We print n - i spaces to push stars to the right, creating the pyramid shape.
Step 2: Stars increase each line
We print i stars with spaces after each to form the pyramid's width.
Step 3: Use of nested repetition
Using repeat() makes printing spaces and stars simple and clean.
Alternative Approaches
Using nested for loops
javascript
const n = 5; for(let i = 1; i <= n; i++) { let row = ''; for(let j = 1; j <= n - i; j++) { row += ' '; } for(let k = 1; k <= i; k++) { row += '* '; } console.log(row); }
This approach uses explicit loops for spaces and stars, which is more verbose but clear for beginners.
Using Array fill and join
javascript
const n = 5; for(let i = 1; i <= n; i++) { const spaces = Array(n - i).fill(' ').join(''); const stars = Array(i).fill('* ').join(''); console.log(spaces + stars); }
This uses arrays to build strings, which can be more flexible but slightly more complex.
Complexity: O(n^2) time, O(n) space
Time Complexity
The program uses nested loops: one for each line and one for printing stars/spaces, resulting in O(n^2) time.
Space Complexity
The extra space is mainly for the string holding each line, which is O(n) in length.
Which Approach is Fastest?
Using String.repeat() is generally faster and cleaner than nested loops for building strings.
| Approach | Time | Space | Best For |
|---|---|---|---|
| String.repeat() | O(n^2) | O(n) | Clean and efficient string building |
| Nested for loops | O(n^2) | O(n) | Clear logic for beginners |
| Array fill and join | O(n^2) | O(n) | Flexible string construction |
Use
String.repeat() to easily print repeated characters like spaces and stars.Beginners often forget to add spaces after stars, making the pyramid look squished.