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 Object-Oriented JavaScript: Challenge Building Constructor Methods and Generating Objects createSpaces() Method Solution

Raymond Popsie
Raymond Popsie
10,703 Points

Does this alternative solution work?

class Board { constructor() { this.rows = 6; this.columns = 7; this.spaces = this.createSpaces(); } /**

  • Generates 2D array of spaces.
  • @return {Array} An array of space objects */

    createSpaces() { const spaces = [];

    for (let i = 0; i <= this.rows; i++) {
        let spaces = new Space(i, undefined);
    for (let i = 0; i <= this.columns; i++) {
        let spaces = new Space(undefined,i);
    return spaces;

    } }

1 Answer

Steven Parker
Steven Parker
220,414 Points

Did you try this? I would not expect it to work for a few reasons:

  • spaces are intended to be created by passing in both coordinates but this code only provides one of them
  • the columns should be arrays of spaces, not just individual spaces
  • the loops would need to be nested to create the column arrays (as shown in the example)