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

practicing Objects

This is some code from the practicing Objects video, the teacher wrote the first function that iterates over a two dimensional array I wondered if mine will do the same thing as hers?

    drawHTMLBoard() {
    for (let column of this.spaces) {
      for(let space of column) {
         space.drawSVGSpace();
      }
    }
    }



/*
   Altenate code dor drawHTMLBoard()
     // need a double for loop to go through the 2D array

    drawHTMLBoard() {
     for(let i= 0; i< this.spaces.length; i++) {
       for(j = 0; j < this.spaces[i][j].length; j++){
         this.spaces[i][j].drawSVGSpace();

       }
     }
  }
*/

1 Answer

Steven Parker
Steven Parker
229,732 Points

You're close, but I see two issues:

  • to avoid implicit declaration of a global, add "let" to the 2nd loop
  • use only one index to access the length for the 2nd loop
       for(j = 0; j < this.spaces[i][j].length; j++){    // original
       for(let j = 0; j < this.spaces[i].length; j++) {  // fixed