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

Mary Healy
Mary Healy
7,818 Points

How to test the createSpaces() method?

hi there,

Just wondering if there is a way to test the createSpaces() method to see if its working? I like to test without looking at the solution as it can really help my understanding but struggling to find a way to test. I tried the following in node but get the following error. I think this is because I have not imported/rendered from Space.js.

js:Board.js
/Users/maryhealy/website_projects/four_in_row/js/Board.js:5
    this.spaces = createSpaces()
    ^

ReferenceError: createSpaces is not defined
    at new Board (/Users/maryhealy/website_projects/four_in_row/js/Board.js:5:5)
    at Object.<anonymous> (/Users/maryhealy/website_projects/four_in_row/js/Board.js:32:14)
    at Module._compile (internal/modules/cjs/loader.js:734:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:745:10)
    at Module.load (internal/modules/cjs/loader.js:626:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:566:12)
    at Function.Module._load (internal/modules/cjs/loader.js:558:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:797:12)
    at executeUserCode (internal/bootstrap/node.js:526:15)
    at startMainThreadExecution (internal/bootstrap/node.js:439:3)

here is my code

class Board {
  constructor(){
    this.rows = 6;
    this.columns = 7;
    this.spaces = createSpaces()
    /** Space objects all belong to the Board, and
    are stored inside a property on the Board object.
    That's why they are created inside the Board class. **/
  }

  /**
 * Generates 2D array of spaces.
 * @return  {Array}     An array of space objects
 */

  createSpaces() {
    const spaces = [];

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

};

const test = new Board();

test.createSpaces()

2 Answers

Sean T. Unwin
Sean T. Unwin
28,690 Points

Hi Mary,

The issue is since createSpaces is a method of the Board class and we're calling that method from the constructor, we need to preface any Class properties or methods with this.

This is referenced in the error where it states that ReferenceError: createSpaces is not defined.

The line highlighted in the error should be:

    this.spaces = this.createSpaces(); // <-- Note: this.createSpaces();

You see this towards the end of the video (last 15 seconds).

Happy coding! :smile:

Mary Healy
Mary Healy
7,818 Points

Thanks Sean, That was a silly mistake. but i got another error and I think this time it is because I have not imported/rendered from Space.js as 'Space is not defined'

/Users/maryhealy/website_projects/four_in_row/js/Board.js:22 let space = new Space (i, j); ^

ReferenceError: Space is not defined at Board.createSpaces (/Users/maryhealy/website_projects/four_in_row/js/Board.js:22:21) at new Board (/Users/maryhealy/website_projects/four_in_row/js/Board.js:5:24) at Object.<anonymous> (/Users/maryhealy/website_projects/four_in_row/js/Board.js:32:14) at Module._compile (internal/modules/cjs/loader.js:734:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:745:10) at Module.load (internal/modules/cjs/loader.js:626:32) at tryModuleLoad (internal/modules/cjs/loader.js:566:12) at Function.Module._load (internal/modules/cjs/loader.js:558:3) at Function.Module.runMain (internal/modules/cjs/loader.js:797:12) at executeUserCode (internal/bootstrap/node.js:526:15)

Sean T. Unwin
Sean T. Unwin
28,690 Points

Your assumption seems correct to me, Mary.

It also appears you are testing this via Nodejs and not in a browser, which if fine, but in order import Space.js you will need to know how module importing/exporting works in Nodejs in order to do that.

Edit:

On further reflection, you should be able to place the following to the top of Board.js:

let fs = require('fs');
eval(fs.readFileSync('./Space.js').toString());

NOTE: an issue with this 'cheap' way of importing in Nodejs, is that eval() won't work with use strict and if there are errors in Space.js you won't be able to discern the errors came from that file directly. This is not a production worthy method; it's a hack to get through a small testing phase.

Mary Healy
Mary Healy
7,818 Points

thanks Sean!! that helps