You're writing up code before you have studied the problem sufficiently. Move away from the keyboard, get a paper and pencil/pen, and do it by hand, several times. Study what steps you are taking, to do this.

How does the row relate to the number of stars in that row? Think of the each loop as a completely separate entity. Focus on each loop, and what the variables are doing to make this diagram print out right.

Break it all down, find the relationships you need. Don't just "do it". Study it. You can't code up a problem until you can sufficiently understand it.