Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Build a Two-Dimensional Array

Maxim Melnic
Maxim Melnic
4,178 Points

var card = [ranks[i], suites[l]];

Hi, i can use this method?

function createDeck() { var suites = ['♠︎','♣︎','♥︎','♦︎']; var ranks = ['Ace','King','Queen','Jack','10','9','8','7','6','5','4', '3','2']; var deck = []; // add your code below here: for (var l = 0; l < suites.length; l += 1) { for (var i = 0; i < ranks.length; i += 1) { var card = [ranks[i], suites[l]]; deck.push(card); ...

in video with solution

let card = []; card.push(ranks[i], suites[l]);

or this line var card = [ranks[i], suites[l]]; add array in array and in array? It's work but can there be a mistake in this?

1 Answer

Using 'i' and 'I' as loop variables for nested for loops can be risky. As you mention, it would be easy to make a mistake. I suppose it is not much different from the use of 'i' and 'j' that is common. The biggest problem is that 'I' and '1' look very similar in some fonts. Try to make your code easy to read.