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 Building Constructor Methods and Generating Objects createTokens() Method Solution

Why use a token variable and not just push the i to the new array?

Why use:

for (let i = 0; i < num; i++) { let token = new Token(i, this); tokens.push(token); }

and not just:

for (i = 0; i < num; i++) { tokens.push(i); }

1 Answer

tokens is an array of token objects. A token has owner, id, dropped and columnLocation properties. Try adding console.log(tokens) to createTokens(num) to see the difference.

The first code creates a token object and adds that to the array. For i = 0 you have:

0: Token {owner: Player, id: "token-0-1", dropped: false, columnLocation: 0}

i is an integer. The second code (modified to let i = 0) just adds the integer. For i = 0 you have:

0: 0