Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript: Challenge Rendering the Game Rendering the Spaces, Board, and Tokens Solution

Why Loop twice in this fashion?

I'm curious as to the reason for looping through twice. If all the spaces are returned to the spaces property in the Board class object via the createSpaces() function, could you just loop through that array of spaces once? Why loop through columns and spaces?

1 Answer

Steven Parker
Steven Parker
231,269 Points

I'm not sure what you are referring to regarding "looping through twice". Are you asking about the nested loops in the createSpaces() function? If so, the outer loop only executes once, and the inner loop executes several times, once for each column.

That's because the inner loop only populates a single column, but the outer loop calls it repeatedly to fill every column.

Did that answer the question? If you were asking about something else perhaps you could give specific time indexes into the video.

Ah yes, sorry I wasn't clear in my question. I was asking about the nested loop. But yes, I get it now. Thank you.