Simple Pattern Programs
Yesterday, I practiced some basic pattern problems using nested loops in JavaScript. Although I missed posting the update, hereโs a quick recap of what I learned.
๐น 1. Square Pattern
I started with a simple square pattern where each row and column prints the same character:
// Example 1
* * * *
* * * *
* * * *
* * * *
๐ What I learned:
**i** represents the **row**
**j** represents the **column**
for (let i = 0; i < 4; i++) {
let row = "";
for (let j = 0; j < 4; j++) {
row += "* ";
}
console.log(row);
}
๐น 2. Incremental Number Pattern
// Example 2
1 2 3
1 2 3
1 2 3
for (let i = 1; i <= 3; i++) {
let row = "";
for (let j = 1; j <= 3; j++) {
row += j + " ";
}
console.log(row);
}
๐งฉ Observation:
Again, i = rows, j = columns. The column value (j) is whatโs changing inside the row.
โ๏ธ Key Takeaway:
Understanding how nested loops work is key to solving pattern problems. The outer loop usually controls the rows, and the inner loop handles the columns.
More patterns coming soon in my DSA practice journey. Happy coding! ๐
Top comments (0)