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 Build a Two-Dimensional Array

function running order ?

for (let i=0; i<suites.length; i++) {
   for (let j=0; j<ranks.length; j++) {
     let card = [];
     card.push(ranks[j], suites[i]);
     deck.push(card);
   }
}

The running order is outer to inner or inner to outer?
What I mean is, if i have a let under ranks.length, suites.length loop can catch the value too?

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

outer to inner. the suites loop begins with i at 0, the first instruction is the ranks loop with j at 0, and ranks will continue to run until finished, at which point suites will tick up to i = 1, then ranks will be run again with j = 0. anything initialized in the inner loop will not be visible in the outer loop. if you need something to be visible in both it needs to be initialized in the other loop or outside of that in the global scope (like deck is initialized outside of these loops). also if this is cards they are called suits, not suites.