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

Birthe Vandermeeren
Birthe Vandermeeren
17,146 Points

My createSpaces() method, using spaces[x] instead of .push(column)

Here's my code for the createSpaces() method:

createSpaces() {
        const spaces = [];

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

            for (let y = 0; y < this.rows; y++){
                spaces[x][y] = new Space(x, y);
            }
        }

        return spaces;
    }
Siddharth Pande
Siddharth Pande
9,046 Points

My head bursts into flames after trying to understand your code. Please explain how it works?

Ian Ostrom
seal-mask
.a{fill-rule:evenodd;}techdegree
Ian Ostrom
Full Stack JavaScript Techdegree Student 10,331 Points

Siddharth Pande, the unfamiliar part in her code is probably the use of spaces[x][y]. This references a location in the 2D array, which is one outer array with x number of inner arrays. The [x] is the index (location) in the outer array and the [y] is the location in the inner array.

Here's a sketch that might help clarify how it works.

I like your method, you create just one variable instead of two and you don't use any push methods as you directly assign new spaces to their correct place in the two dimensional array!

Well I'm creating an Array object for each column with predefined length, not sure which method is more convenient though !

    createSpaces() {
        const listOfSpaces = new Array(this.columns);
        for (let x = 0; x < this.columns; x++) {
            listOfSpaces[x] = new Array(this.rows);
            for (let y = 0; y < this.rows; y++)
                listOfSpaces[x][y] = new Space(x, y);
        }
        return listOfSpaces;
    }

I just want to say that I did something similar, and when seeing the solution thought I was completely off, and was misunderstanding a lot. It's a massive relief to know I wasn't far off with my thinking.

1 Answer

Birthe Vandermeeren
Birthe Vandermeeren
17,146 Points

Hi Siddharth Pande,

I do the following:

  • I create an empty array called spaces
  • For each column, I create an empty array inside the spaces array
  • Inside each column array in the spaces array, I create a new space for each row
  • Now the spaces array exists of as many arrays as there are columns, and each column array has as many spaces as there are rows.

Not sure if this clarifies things?