DEV Community

Laxman Nemane
Laxman Nemane

Posted on

๐Ÿง  DSA Series - Day 4

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);
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น 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);
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฉ 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)