Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript Object Basics Filling Out the Play Method

Spencer Renfro
PLUS
Spencer Renfro
Courses Plus Student 10,261 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
218,861 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 10,261 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
218,861 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