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++;
}
Output:
12345
Pattern 2: Print 54321
Code:
let i = 5;
while (i >= 1) {
document.write(i);
i--;
}
Output:
54321
Pattern 3: Print 369
Code:
let i = 3;
while (i <= 9) {
document.write(i);
i += 3;
}
Output:
369
Pattern 4: Print 531
Code:
let i = 5;
while (i >= 1) {
document.write(i);
i -= 2;
}
Output:
531
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.
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.