Pattern problems are classic programming exercises that help you master nested loops, string manipulation, and logical thinking. They're not just for interviews — they build the foundational skills needed to work with grids, matrices, UI layouts, and data visualization. This tutorial covers the most essential patterns and the strategies to solve them.
Before jumping into code, follow this systematic approach:
- How many rows?
- How many columns in each row?
- What changes from row to row?
- What's the relationship between row number and the output?
- Outer loop: controls rows (usually
i) - Inner loop: controls columns (usually
j)
- What's printed at position
(row, col)? - Is it based on row number, column number, or both?
- Write the loops
- Add the print logic
- Test with small inputs first
*
**
***
****
*****
Analysis:
- Row 1: 1 star
- Row 2: 2 stars
- Row
i:istars
function rightAngledTriangle(n) {
for (let i = 1; i <= n; i++) {
let row = "";
for (let j = 1; j <= i; j++) {
row += "*";
}
console.log(row);
}
}
rightAngledTriangle(5);Alternative (using repeat):
function rightAngledTriangle(n) {
for (let i = 1; i <= n; i++) {
console.log("*".repeat(i));
}
}*****
****
***
**
*
Analysis:
- Row 1:
nstars - Row 2:
n-1stars - Row
i:n - i + 1stars
function invertedTriangle(n) {
for (let i = 1; i <= n; i++) {
console.log("*".repeat(n - i + 1));
}
}
invertedTriangle(5); *
***
*****
*******
*********
Analysis:
- Row 1: 4 spaces, 1 star
- Row 2: 3 spaces, 3 stars
- Row
i:n - ispaces,2*i - 1stars
function pyramid(n) {
for (let i = 1; i <= n; i++) {
let spaces = " ".repeat(n - i);
let stars = "*".repeat(2 * i - 1);
console.log(spaces + stars);
}
}
pyramid(5);*********
*******
*****
***
*
Analysis:
- Row 1: 0 spaces,
2*n - 1stars - Row 2: 1 space,
2*n - 3stars - Row
i:i - 1spaces,2*(n - i) + 1stars
function invertedPyramid(n) {
for (let i = 1; i <= n; i++) {
let spaces = " ".repeat(i - 1);
let stars = "*".repeat(2 * (n - i) + 1);
console.log(spaces + stars);
}
}
invertedPyramid(5); *
***
*****
*******
*********
*******
*****
***
*
Combine the pyramid and inverted pyramid (excluding the middle row twice):
function diamond(n) {
// Upper half (including middle)
for (let i = 1; i <= n; i++) {
let spaces = " ".repeat(n - i);
let stars = "*".repeat(2 * i - 1);
console.log(spaces + stars);
}
// Lower half
for (let i = n - 1; i >= 1; i--) {
let spaces = " ".repeat(n - i);
let stars = "*".repeat(2 * i - 1);
console.log(spaces + stars);
}
}
diamond(5);1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Analysis:
- Row
i: numbers from 1 toi
function numberTriangle(n) {
for (let i = 1; i <= n; i++) {
let row = "";
for (let j = 1; j <= i; j++) {
row += j + " ";
}
console.log(row.trim());
}
}
numberTriangle(5);1
2 3
4 5 6
7 8 9 10
Analysis:
- Use a counter that increments across all rows
function floydsTriangle(n) {
let num = 1;
for (let i = 1; i <= n; i++) {
let row = "";
for (let j = 1; j <= i; j++) {
row += num + " ";
num++;
}
console.log(row.trim());
}
}
floydsTriangle(4); 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Each number is the sum of the two directly above it.
function pascalsTriangle(n) {
for (let i = 0; i < n; i++) {
let row = "";
// Leading spaces
row += " ".repeat(n - i - 1);
let num = 1;
for (let j = 0; j <= i; j++) {
row += num + " ";
num = num * (i - j) / (j + 1);
}
console.log(row);
}
}
pascalsTriangle(5); 1
232
34543
4567654
567898765
Analysis:
- Row
istarts with numberi - Increases to
2*i - 1 - Then decreases back
function numberPyramid(n) {
for (let i = 1; i <= n; i++) {
let spaces = " ".repeat(n - i);
let row = spaces;
// Increasing part
for (let j = i; j < 2 * i; j++) {
row += j;
}
// Decreasing part
for (let j = 2 * i - 2; j >= i; j--) {
row += j;
}
console.log(row);
}
}
numberPyramid(5);*****
* *
* *
* *
*****
function hollowSquare(n) {
for (let i = 1; i <= n; i++) {
let row = "";
for (let j = 1; j <= n; j++) {
if (i === 1 || i === n || j === 1 || j === n) {
row += "*";
} else {
row += " ";
}
}
console.log(row);
}
}
hollowSquare(5); *
* *
* *
* *
* *
* *
* *
* *
*
function hollowDiamond(n) {
// Upper half
for (let i = 1; i <= n; i++) {
let spaces = " ".repeat(n - i);
if (i === 1) {
console.log(spaces + "*");
} else {
let middleSpaces = " ".repeat(2 * i - 3);
console.log(spaces + "*" + middleSpaces + "*");
}
}
// Lower half
for (let i = n - 1; i >= 1; i--) {
let spaces = " ".repeat(n - i);
if (i === 1) {
console.log(spaces + "*");
} else {
let middleSpaces = " ".repeat(2 * i - 3);
console.log(spaces + "*" + middleSpaces + "*");
}
}
}
hollowDiamond(5);* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
function butterfly(n) {
// Upper half
for (let i = 1; i <= n; i++) {
let stars = "*".repeat(i);
let spaces = " ".repeat(2 * (n - i));
console.log(stars + spaces + stars);
}
// Lower half
for (let i = n - 1; i >= 1; i--) {
let stars = "*".repeat(i);
let spaces = " ".repeat(2 * (n - i));
console.log(stars + spaces + stars);
}
}
butterfly(5); * *
* * * *
* * *
function zigZag(n) {
for (let i = 1; i <= 3; i++) {
let row = "";
for (let j = 1; j <= n; j++) {
if ((i + j) % 4 === 0 || (i === 2 && j % 4 === 0)) {
row += "*";
} else {
row += " ";
}
}
console.log(row);
}
}
zigZag(9);1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
function binaryTriangle(n) {
for (let i = 1; i <= n; i++) {
let row = "";
for (let j = 1; j <= i; j++) {
row += ((i + j) % 2 === 0 ? "1" : "0") + " ";
}
console.log(row.trim());
}
}
binaryTriangle(5);*********
*******
*****
***
*
***
*****
*******
*********
function hourglass(n) {
// Upper inverted pyramid
for (let i = n; i >= 1; i--) {
let spaces = " ".repeat(n - i);
let stars = "*".repeat(2 * i - 1);
console.log(spaces + stars);
}
// Lower pyramid
for (let i = 2; i <= n; i++) {
let spaces = " ".repeat(n - i);
let stars = "*".repeat(2 * i - 1);
console.log(spaces + stars);
}
}
hourglass(5);| Pattern Element | Formula |
|---|---|
Stars in row i (pyramid) |
2*i - 1 |
Spaces in row i (centered) |
n - i |
Stars in row i (inverted) |
2*(n-i) + 1 |
Numbers in row i |
1 to i |
Total elements in row i |
Usually equals row number |
- Start with a small value (like
n=3orn=4) to trace through manually - Draw a grid and label row and column numbers
- Look for symmetry — many patterns are mirrored
- Break complex patterns into simpler ones (e.g., diamond = pyramid + inverted pyramid)
- Use
repeat()for cleaner code when possible - Test edge cases:
n=1,n=0, largen
1
12
123
1234
12345
1
121
12321
1234321
123454321
Fill an n x n matrix in spiral order and print it:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
1 1
12 21
123 321
1234 4321
1234554321
*****
*****
*****
*****
*****
- Pattern problems train nested loop control and logical reasoning
- Always analyze: rows, columns, spaces, and values
- Common formulas:
2*i-1stars,n-ispaces - Combine simple patterns to build complex ones
- Use string methods like
repeat()for cleaner code - Practice is key — start with simple patterns and work up
Apply your pattern skills to:
- Problem Solving on Loops — algorithmic challenges
- Arrays and Matrices — 2D data structures
- Object-Oriented JavaScript — organizing complex logic
Happy coding! 🚀