In that case the first row that is printed has no star, it’s empty. The reason for that is in the second for-loop stars is not smaller than row, it’s equal. Therefore no star is printed because pattern+= ‘*’; is not executed. Variabele pattern is still empty.
The next run row = 1 so stars is smaller than row and pattern+= ‘*’; is executed which results in one printed star.
for (let i = 1; i <= rows; i++) {
stars += '*';
console.log(stars);
}
allow you to have two lines of codes in your for loop. Without the curly braces, your console.log wouldn’t be part of your loop. The nested curly braces in the first example make it possible to have a for loop within a for loop, which, as you’ve discovered, is not necessary to solve this stars problem. Yours works because you are re-using the stars list and just adding one more star each time. In the first example, they start from scratch with a new stars list each time, so they need a second for loop just to rebuild the stars. I think your solution is simpler and easier to understand.
Just finished this exercise and also noticed my implementation used only one for loop. Thought I would come to the forum to see if anyone else came out with the same. Good job us!!
I used only one loop also. Am I missing something? Is there a reason he chose this example to explain a nested loop? Is it possible without a nested loop the code could break? Is there a specific reason to start from scratch and rebuild the list each time?