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

Armend Azemi
Armend Azemi
27,921 Points

My solution, thoughts?

So, as the title says, this is my solution, and curious about your thoughts and perhaps some suggestions to improve it.

The biggest issue I have right now with this code is that #moveRight() and #moveLeft() are very similar and I would like to have them in one function.

The main reason for my refactoring was to clean up the Game.js file a bit and not have too many if statements in there and just make the code a bit more readable in general. Another change I made was, that when the player reaches the end of the board, either left or right, and continues moving against the boundary they move over to the other side of the board, making the game a bit more fluent.

Game.js

 handleKeyDown(e){
        if(this.ready){
            let activeToken = this.activePlayer.activeToken
            activeToken.move(e.key, this.board.columns)
        }
    }

Token.js

 move (key, columns){
    switch(key) {
     case 'ArrowLeft':
       this.#moveLeft(columns)
        break
      case 'ArrowRight':
        this.#moveRight(columns)
        break
      }
    }

Token.js (Private functions, only accssible inside Token.js)

#moveLeft(numOfColumns){
    if(this.columnLocation === 0){
      this.htmlToken.style.left = (numOfColumns-1) * 76
      this.columnLocation = numOfColumns - 1 
    }else {
      this.htmlToken.style.left = this.offsetLeft - 76
      this.columnLocation -= 1
    }
  }

  #moveRight(numColumns){
    if(this.columnLocation === numColumns-1){
      this.htmlToken.style.left = 0
      this.columnLocation = 0
    }else {
      this.htmlToken.style.left = this.offsetLeft + 76
      this.columnLocation += 1
    }
  }

1 Answer

Steven Parker
Steven Parker
229,005 Points

You could combine #moveRight() and #moveLeft() into a single function by adding an additional parameter to indicate direction. But I'm not sure this would make the code more compact or enhance readability.

Also note that it's a "best practice" to terminate statements with semicolons and consistently using this will in itself enhance readability.

Armend Azemi
Armend Azemi
27,921 Points

Yeah, I guess I agree to a point, essentially just moving the code around, but I just preferred having the logic in the token class. And coming from a Java background I'm used to using the semi-colons, it's just that I'm a bit lazy and the convention we followed in my University was doing it without, but I'll probably start using it for readability like you said.

Thanks for the answer and happy coding!