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 Adding the Game Logic Build the mark() Method

anmo20
anmo20
6,470 Points

How do I access the token id from within the Space class?

Really confused as to how I access the id of the current token dropped into the space, when the Space class doesn't have references to any of the other Classes. The other classes have references to other classes by way of using someVariable = new Board(); or whatever the case may be, but I don't want the "mark" method to create a NEW object, do I?

I feel like this would be much easier achieved by just setting the "token" value of the space the token was dropped into by adding in the playToken90 method from the previous step.

whateverTheChosenSpaceWas.token = this.activePlayer.activeToken.id;

2 Answers

You don't. The token should be passed as a parameter to the mark() method. That's what's indicated in the JDoc by the @param line:

* @param {Object} token - The dropped token

wildgoosestudent
wildgoosestudent
11,274 Points

I thought similar to you so wanted to test it out to make sure I was setting the space to be the current token (which I think is a bit more than what this step asks for but I always like to make sure I have as full an understanding of the code as possible before moving on.

In Game.js

    let spaces = this.board.spaces;
    let activeToken = this.activePlayer.activeToken;
    let currentColumn = spaces[activeToken.columnLocation];

    for (let i = 5; i >= 0; i--) {
      if (currentColumn[i].token == null) {
        const targetSpace = currentColumn[i]

        this.ready = false;
        activeToken.drop(targetSpace)
        // below is the line that takes the space, calls the mark method, 
        // and sends the method the active token
        targetSpace.mark(activeToken)
        break
      } 
    }
  }

In Space.js

  mark(activeToken) {
    // receives the activeToken we sent from Game.js
    // and sets the Space object's token property
    this.token = activeToken;
    // I like to console.log the results to make sure it works!
    console.log(this.token)
  }