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 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);
        spaces.push(space);
    
    for (let i = 0; i <= this.columns; i++) {
        let spaces = new Space(undefined,i);
        spaces.push(space);
    }
    return spaces;
    

    } }

1 Answer

Steven Parker
Steven Parker
231,271 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)