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 Making the Game Interactive moveLeft() and moveRight() Methods Solution

Julianna Kahn
Julianna Kahn
20,702 Points

I don't really understand this chunk of code.

moveRight(columns) { if (this.columnLocation < columns - 1) { this.htmlToken.style.left = this.offsetLeft + 76; this.columnLocation +=1; } } I see that columns is a property in the Board class but I don't understand where it gets defined in Token, Also, I don't see where htmlToken gets associated with x and y values such that the column location can be offset on the x-axis.

2 Answers

Steven Parker
Steven Parker
231,275 Points

In this method, "columns" is a parameter name and not related to the property in the "Board" class. It get's its value from the argument passed when "moveRight" gets called elsewhere in the code.

The token's "x" location is stored in the "offsetLeft" property, and "y" isn't being used here.


Update: All the "htmlToken" method does is return a reference to the HTML element that provides the visual image of the token. The element has style properties that are used for positioning, and the "left" property is being adjusted by this method ("moveRight").

Steven Parker
Steven Parker
231,275 Points

The properties of "htmlToken" (including "offsetLeft") are established by the browser when the element is created.

When tokens are created (in "drawHTMLToken"), they are placed inside the div with the ID of "game-board-underlay", initially at the left side by default.

Julianna Kahn
Julianna Kahn
20,702 Points

So, if I correctly understand, the initial x-value for the token location is defined to exist in the first column, all the way at the left of the board. That helps a lot because I couldn't see how we knew where it was before it was first moved. Thanks.

Julianna Kahn
Julianna Kahn
20,702 Points

You're saying that the columns parameter being passed, has nothing to do with columns defined in the Board constructor.

I understand that there is no "y" coordinate. But I am still uncertain of where the htmlToken() method gets the data it needs to define the token location/coordinates on the board.

Thanks

Julianna Kahn
Julianna Kahn
20,702 Points

So we have a reference to the visual image of the token. My understanding is that .offset - 76 means the token would be physically positioned 76 pixels to the left, along the x axis. But what/where is the code that defines the original positioning of the token?