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

Proud of my code, should I be?

At the risk of sounding like I'm bigging myself up... I'm happy with the job I did, but I'm wondering about the pros and cons of his methods vs mine. For some reason I was determined to use forEach instead of normal for loops with indexes, so my code is quite different. Any feedback would be appreciated!

function shuffle(arr) {
  let j, x, i;
  for (i = arr.length - 1; i > 0; i--) {
      j = Math.floor(Math.random() * (i + 1));
      x = arr[i];
      arr[i] = arr[j];
      arr[j] = x;
  }
  return arr;
}

function createDeck() {
  let suites = ['♠︎','♣︎','♥︎','♦︎'];
  let ranks = ['Ace','King','Queen','Jack','10','9','8','7','6','5','4','3','2'];
  let deck = [];
  let card=[];
  suites.forEach( suite => ranks.forEach( rank =>  deck.push([`${rank}`,`${suite}`]) ));
  return shuffle(deck);  
}

myDeck = createDeck();
myDeck.forEach( card => console.log(`${card[0]} of ${card[1]}`));