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

Leonardo Motta
Leonardo Motta
11,284 Points

A bit confused here

This is my output with the instructor function, it creates two board??

[  
  [ 
    Space { x: 0, y: 0, id: 'space-0-0', token: null },
    Space { x: 0, y: 1, id: 'space-0-1', token: null },
    Space { x: 0, y: 2, id: 'space-0-2', token: null },
    Space { x: 0, y: 3, id: 'space-0-3', token: null },
    Space { x: 0, y: 4, id: 'space-0-4', token: null },
    Space { x: 0, y: 5, id: 'space-0-5', token: null } 
  ],
  [ 
    Space { x: 1, y: 0, id: 'space-1-0', token: null },
    Space { x: 1, y: 1, id: 'space-1-1', token: null },
    Space { x: 1, y: 2, id: 'space-1-2', token: null },
    Space { x: 1, y: 3, id: 'space-1-3', token: null },
    Space { x: 1, y: 4, id: 'space-1-4', token: null },
    Space { x: 1, y: 5, id: 'space-1-5', token: null } 
  ],
  [ 
    Space { x: 2, y: 0, id: 'space-2-0', token: null },
    Space { x: 2, y: 1, id: 'space-2-1', token: null },
    Space { x: 2, y: 2, id: 'space-2-2', token: null },
    Space { x: 2, y: 3, id: 'space-2-3', token: null },
    Space { x: 2, y: 4, id: 'space-2-4', token: null },
    Space { x: 2, y: 5, id: 'space-2-5', token: null } 
  ],
  [ 
    Space { x: 3, y: 0, id: 'space-3-0', token: null },
    Space { x: 3, y: 1, id: 'space-3-1', token: null },
    Space { x: 3, y: 2, id: 'space-3-2', token: null },
    Space { x: 3, y: 3, id: 'space-3-3', token: null },
    Space { x: 3, y: 4, id: 'space-3-4', token: null },
    Space { x: 3, y: 5, id: 'space-3-5', token: null } 
  ],
  [ 
    Space { x: 4, y: 0, id: 'space-4-0', token: null },
    Space { x: 4, y: 1, id: 'space-4-1', token: null },
    Space { x: 4, y: 2, id: 'space-4-2', token: null },
    Space { x: 4, y: 3, id: 'space-4-3', token: null },
    Space { x: 4, y: 4, id: 'space-4-4', token: null },
    Space { x: 4, y: 5, id: 'space-4-5', token: null } 
  ],
  [ 
    Space { x: 5, y: 0, id: 'space-5-0', token: null },
    Space { x: 5, y: 1, id: 'space-5-1', token: null },
    Space { x: 5, y: 2, id: 'space-5-2', token: null },
    Space { x: 5, y: 3, id: 'space-5-3', token: null },
    Space { x: 5, y: 4, id: 'space-5-4', token: null },
    Space { x: 5, y: 5, id: 'space-5-5', token: null } 
  ],
  [ 
    Space { x: 6, y: 0, id: 'space-6-0', token: null },
    Space { x: 6, y: 1, id: 'space-6-1', token: null },
    Space { x: 6, y: 2, id: 'space-6-2', token: null },
    Space { x: 6, y: 3, id: 'space-6-3', token: null },
    Space { x: 6, y: 4, id: 'space-6-4', token: null },
    Space { x: 6, y: 5, id: 'space-6-5', token: null } 
  ] 
],
[ 
  [ 
    Space { x: 0, y: 0, id: 'space-0-0', token: null },
    Space { x: 0, y: 1, id: 'space-0-1', token: null },
    Space { x: 0, y: 2, id: 'space-0-2', token: null },
    Space { x: 0, y: 3, id: 'space-0-3', token: null },
    Space { x: 0, y: 4, id: 'space-0-4', token: null },
    Space { x: 0, y: 5, id: 'space-0-5', token: null } 
  ],
  [
    Space { x: 1, y: 0, id: 'space-1-0', token: null },
    Space { x: 1, y: 1, id: 'space-1-1', token: null },
    Space { x: 1, y: 2, id: 'space-1-2', token: null },
    Space { x: 1, y: 3, id: 'space-1-3', token: null },
    Space { x: 1, y: 4, id: 'space-1-4', token: null },
    Space { x: 1, y: 5, id: 'space-1-5', token: null } 
  ],
  [ 
    Space { x: 2, y: 0, id: 'space-2-0', token: null },
    Space { x: 2, y: 1, id: 'space-2-1', token: null },
    Space { x: 2, y: 2, id: 'space-2-2', token: null },
    Space { x: 2, y: 3, id: 'space-2-3', token: null },
    Space { x: 2, y: 4, id: 'space-2-4', token: null },
    Space { x: 2, y: 5, id: 'space-2-5', token: null } 
  ],
  [ 
    Space { x: 3, y: 0, id: 'space-3-0', token: null },
    Space { x: 3, y: 1, id: 'space-3-1', token: null },
    Space { x: 3, y: 2, id: 'space-3-2', token: null },
    Space { x: 3, y: 3, id: 'space-3-3', token: null },
    Space { x: 3, y: 4, id: 'space-3-4', token: null },
    Space { x: 3, y: 5, id: 'space-3-5', token: null } 
  ],
  [
    Space { x: 4, y: 0, id: 'space-4-0', token: null },
    Space { x: 4, y: 1, id: 'space-4-1', token: null },
    Space { x: 4, y: 2, id: 'space-4-2', token: null },
    Space { x: 4, y: 3, id: 'space-4-3', token: null },
    Space { x: 4, y: 4, id: 'space-4-4', token: null },
    Space { x: 4, y: 5, id: 'space-4-5', token: null }
  ],
  [ 
    Space { x: 5, y: 0, id: 'space-5-0', token: null },
    Space { x: 5, y: 1, id: 'space-5-1', token: null },
    Space { x: 5, y: 2, id: 'space-5-2', token: null },
    Space { x: 5, y: 3, id: 'space-5-3', token: null },
    Space { x: 5, y: 4, id: 'space-5-4', token: null },
    Space { x: 5, y: 5, id: 'space-5-5', token: null } 
  ],
  [ 
    Space { x: 6, y: 0, id: 'space-6-0', token: null },
    Space { x: 6, y: 1, id: 'space-6-1', token: null },
    Space { x: 6, y: 2, id: 'space-6-2', token: null },
    Space { x: 6, y: 3, id: 'space-6-3', token: null },
    Space { x: 6, y: 4, id: 'space-6-4', token: null },
    Space { x: 6, y: 5, id: 'space-6-5', token: null } 
  ]
]

1 Answer

Leonardo Motta
Leonardo Motta
11,284 Points

I dont know what I did wrong, but here it is my new test and the output.

class Space {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.id = `space-${x}-${y}`;
    this.token = null;
  }
}

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

  createSpaces() {
    const spaces = [];

    for (let x = 0; x < this.columns; x++) {
      const column = [];

      for (let y = 0; y < this.rows; y++) {
        const space = new Space(x, y);
        column.push(space);
      }

      spaces.push(column);
    }
    return spaces;
  }
}

const myBoard = new Board();

console.log(myBoard.spaces);
[ [ Space { x: 0, y: 0, id: 'space-0-0', token: null },
    Space { x: 0, y: 1, id: 'space-0-1', token: null },
    Space { x: 0, y: 2, id: 'space-0-2', token: null },
    Space { x: 0, y: 3, id: 'space-0-3', token: null },
    Space { x: 0, y: 4, id: 'space-0-4', token: null },
    Space { x: 0, y: 5, id: 'space-0-5', token: null } ],
  [ Space { x: 1, y: 0, id: 'space-1-0', token: null },
    Space { x: 1, y: 1, id: 'space-1-1', token: null },
    Space { x: 1, y: 2, id: 'space-1-2', token: null },
    Space { x: 1, y: 3, id: 'space-1-3', token: null },
    Space { x: 1, y: 4, id: 'space-1-4', token: null },
    Space { x: 1, y: 5, id: 'space-1-5', token: null } ],
  [ Space { x: 2, y: 0, id: 'space-2-0', token: null },
    Space { x: 2, y: 1, id: 'space-2-1', token: null },
    Space { x: 2, y: 2, id: 'space-2-2', token: null },
    Space { x: 2, y: 3, id: 'space-2-3', token: null },
    Space { x: 2, y: 4, id: 'space-2-4', token: null },
    Space { x: 2, y: 5, id: 'space-2-5', token: null } ],
  [ Space { x: 3, y: 0, id: 'space-3-0', token: null },
    Space { x: 3, y: 1, id: 'space-3-1', token: null },
    Space { x: 3, y: 2, id: 'space-3-2', token: null },
    Space { x: 3, y: 3, id: 'space-3-3', token: null },
    Space { x: 3, y: 4, id: 'space-3-4', token: null },
    Space { x: 3, y: 5, id: 'space-3-5', token: null } ],
  [ Space { x: 4, y: 0, id: 'space-4-0', token: null },
    Space { x: 4, y: 1, id: 'space-4-1', token: null },
    Space { x: 4, y: 2, id: 'space-4-2', token: null },
    Space { x: 4, y: 3, id: 'space-4-3', token: null },
    Space { x: 4, y: 4, id: 'space-4-4', token: null },
    Space { x: 4, y: 5, id: 'space-4-5', token: null } ],
  [ Space { x: 5, y: 0, id: 'space-5-0', token: null },
    Space { x: 5, y: 1, id: 'space-5-1', token: null },
    Space { x: 5, y: 2, id: 'space-5-2', token: null },
    Space { x: 5, y: 3, id: 'space-5-3', token: null },
    Space { x: 5, y: 4, id: 'space-5-4', token: null },
    Space { x: 5, y: 5, id: 'space-5-5', token: null } ],
  [ Space { x: 6, y: 0, id: 'space-6-0', token: null },
    Space { x: 6, y: 1, id: 'space-6-1', token: null },
    Space { x: 6, y: 2, id: 'space-6-2', token: null },
    Space { x: 6, y: 3, id: 'space-6-3', token: null },
    Space { x: 6, y: 4, id: 'space-6-4', token: null },
    Space { x: 6, y: 5, id: 'space-6-5', token: null } ] ]