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

Marius Urbonas
Marius Urbonas
7,921 Points

Why this code doesn't work?

Hi there I thought this solution would work for deck creation, but it doesn't run properly, maybe you know how to correct it? Here's my code: var deck = []; var suites = ['♠︎','♣︎','♥︎','♦︎']; var ranks = ['Ace','King','Queen','Jack','10','9','8','7','6','5','4', '3','2']; var i,c,b; for (i= 0,c=0, b=0; deck.length != 52; i++, b++) { deck.push(suites[c]+ranks[b]); if (deck[deck.length -1] === "2" + suites[c] ) { c++; b= 0;

}

};

document.write(deck);

Thomas Rudel
Thomas Rudel
6,379 Points

Hello Marius Urbonas,

I rewrote your code a bit, because after you ran the ranks you loose them and you did not go back to them to combine them wiht another suite. For this I created 2 more for statements. I didn't get what you meant with the if statement so i deleted it. The code works:

var deck = [];
var suites = ['♠︎','♣︎','♥︎','♦︎'];
var ranks = ['Ace','King','Queen','Jack','10','9','8','7','6','5','4', '3','2'];
var i,c,b;

for (i= 0; deck.length != 52; i++) {
  for (b = 0; b < ranks.length; b++) {
    for (c = 0; c < suites.length; c++) {
      deck.push(suites[c]+ranks[b]);
    }
  }
}

console.log(deck);

1 Answer

Adam Beer
Adam Beer
11,314 Points

I think your for loop declaration is invalid, because you want to run too much information with your for(i= 0,c=0, b=0; deck.length != 52; i++, b++) loop.

Syntax

for ([initialization]; [condition]; [final-expression]) statement

initialization

An expression (including assignment expressions) or variable declaration. Typically used to initialize a counter variable. This expression may optionally declare new variables with var or let keywords. Variables declared with var are not local to the loop, i.e. they are in the same scope the for loop is in. Variables declared with let are local to the statement. The result of this expression is discarded.

condition

An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.

final-expression

An expression to be evaluated at the end of each loop iteration. This occurs before the next evaluation of condition. Generally used to update or increment the counter variable.

statement

A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements. To execute no statement within the loop, use an empty statement (;).

Reminder: You can use the markdown cheatsheet. So the code is more transparent

Code

Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

      ```html
      <p>This is code!</p>
      ```