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

why Do we need to come up With an Extra Temporary List

why Do we need to come up With an Extra Temporary List why cant we directly append the space object within the nested for Loop.like this

 spaces(row1,columns1){
        let spaces=[];
        for(let r=0;r<row1;r++)
        {

            for(let c=0;c<columns1;c++)
            {
                spaces.push((new Token(r,c)));
            }

        }
        return spaces;    
    }

1 Answer

Eugene Dianov
Eugene Dianov
4,264 Points

Because we want to create a two-dimensional array. Your code creates a one-dimensional array.

Alternatively solution

createSpaces() {

let spaces = [[]];

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

}