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 Callback Function Solution

Renwell Queyquep
Renwell Queyquep
9,492 Points

Skipped callback function

There is a problem when I use a callback function to answer this question, but it is identical to the code solution in the video. Yet when I tested my own code it skips the callback function.

Shlomi Bittan
Shlomi Bittan
6,718 Points

Hi Renwell Queyquep, Please paste the code you wrote so we can inspect it, otherwise it is hard to tell what is the problem.

Thanks

Renwell Queyquep
Renwell Queyquep
9,492 Points
playToken() {
        let spaces = this.board.spaces;
        let activeToken = this.activePlayer.activeToken;
        let targetColumn = spaces[activeToken.columnLocation];
        let targetSpace = null;

        for (let space of targetColumn) {
            if (space.token === null) {
                targetSpace = space;
            }
        }

        if (targetSpace !== null) {
            const game = this;
            game.ready = false;

            activeToken.drop(targetSpace, function(){
                // I can't run this code
                game.updateGameState(activeToken, targetSpace);           
            });          
        }              
    }

3 Answers

Shlomi Bittan
Shlomi Bittan
6,718 Points

You should check if your 'targetSpace' has any value. Did you debug your code? Put some breake points...maybe here:

if (targetSpace !== null)...
Renwell Queyquep
Renwell Queyquep
9,492 Points

The target space has a value and it goes through the if statement, but it still skips the the updateGameState function

Richard Eldridge
Richard Eldridge
8,229 Points

Perhaps it's a problem with the updateGameState method itself.

The same thing is happening to me. The space is not null.

I tested by setting

  if (targetSpace !== null) {
      const game = this;
      game.ready = false;
      activeToken.drop(targetSpace, function() {
         console.log("Hi");
        //game.updateGameState(activeToken, targetSpace);
      });
    }

and

  drop(target, callback) {
    this.dropped = true;
    this.htmlToken.animate({
      top: `${target.y * target.radius * 2}px`,
      left: `${target.x * target.radius * 2}px`}, 750, 'easeOutBounce', function() { console.log("Hi"); });
  }

And neither of those worked. So animate isn't calling the callback for some reason.

Could this be an issue with

  1. A specific browser (I'm using Mozilla Firefox 104.0.2 on Ubuntu 22.04.1)
  2. A specific Jquery version (I have 3.3.1 from jQuery().jquery on debug console)