Lesson 36

Loop Patterns

Create star patterns with loops

20 min
+25 XP
Step 1 of 5

Pattern 1: Right Triangle

Let's start with the simplest pattern: a right triangle where each row has one more star than the previous row.

The Pattern:

*
**
***
****
*****

The Code:

for (let i = 1; i <= 5; i++) {
  let stars = ""
  for (let j = 1; j <= i; j++) {
    stars += "*"
  }
  console.log(stars)
}

How It Works:

  • • Row 1: Inner loop runs 1 time → print 1 star
  • • Row 2: Inner loop runs 2 times → print 2 stars
  • • Row 3: Inner loop runs 3 times → print 3 stars
  • • The key: Inner loop condition is j <= i

Alternative Method:

You can also use the repeat() method:

for (let i = 1; i <= 5; i++) {
  console.log("*".repeat(i))
}