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 Object Basics Filling Out the Play Method

Spencer Renfro
PLUS
Spencer Renfro
Courses Plus Student 11,133 Points

I need help on the coding challenge of adding an empty if inside the play method

Inside the play method, I am to add an empty if statement that checks if it is the player's turn using dot notation. I do not see why my code is not working

const player1 = { name: 'Ashley', color: 'purple', isTurn: true, play: function(){ if(player1.isTurn === true)

}

}

object.js
const player1 = {
    name: 'Ashley',
    color: 'purple',
    isTurn: true,
    play: function(){
        if(player1.isTurn === true)

    }
} 

2 Answers

Steven Parker
Steven Parker
231,268 Points

The "if" statement isn't complete yet. It should be followed by braces for the code block, even if the contents are temporarily empty.

Also, code inside an object method should not refer to the object itself by name. Instead, it should use the "this" keyword.

And while it won't cause an error, you never need to compare a Boolean value to "true", you can just test it directly.

Spencer Renfro
Spencer Renfro
Courses Plus Student 11,133 Points

The challenge still does not accept it with curly braces, I did think that was my problem but I forgot to add that into the description. I do not think I am familiar with what you mean by the "this" keyword. I also do not understand how to test the if statement directly instead of comparing a boolean to a true or false value

Steven Parker
Steven Parker
231,268 Points

The braces are just one of several issues that must all be fixed for the challenge to pass.

The use of the "this" keyword was discussed in earlier lessons. But as it applies here, instead of using object name to refer to itself (like: player1.isTurn) the keyword is used instead (this.isTurn).

And testing against "true" will work, but it's not necessary:

        if (this.isTurn === true) { }    // instead of this
        if (this.isTurn) { }             // you can just do this