DEV Community

SEENUVASAN P
SEENUVASAN P

Posted on

Day 9 : Learning JavaScript While Loops – Simple Number Patterns

Today I practiced JavaScript and explored how to use while loops to print different number patterns. It was fun and helped me understand how loops work step by step.

Pattern 1: Print 12345

Code:

let i = 1;
while (i <= 5) {
  document.write(i);
  i++;
}
Enter fullscreen mode Exit fullscreen mode

Output:

12345
Enter fullscreen mode Exit fullscreen mode

Pattern 2: Print 54321

Code:

let i = 5;
while (i >= 1) {
  document.write(i);
  i--;
}
Enter fullscreen mode Exit fullscreen mode

Output:

54321
Enter fullscreen mode Exit fullscreen mode

Pattern 3: Print 369

Code:

let i = 3;
while (i <= 9) {
  document.write(i);
  i += 3;
}
Enter fullscreen mode Exit fullscreen mode

Output:

369
Enter fullscreen mode Exit fullscreen mode

Pattern 4: Print 531

Code:

let i = 5;
while (i >= 1) {
  document.write(i);
  i -= 2;
}
Enter fullscreen mode Exit fullscreen mode

Output:

531
Enter fullscreen mode Exit fullscreen mode

What I Learned:

while loops repeat a block of code as long as the condition is true.

We can control the output pattern using the loop’s start value, condition, and increment/decrement.

Practicing small patterns like these helps build logic for bigger problems.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.