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

I don't know how to use Java script dot notation with an if statement. How do I solve this problem?

const player1 = { name: 'Ashley', color: 'purple', isTurn: true, play: function(){ // write code here. (I know the If statement goes here but I don't know how to combine it with java script notation. } }

object.js
const player1 = {
    name: 'Ashley',
    color: 'purple',
    isTurn: true,
    play: function(){
        // write code here.
    }
}

4 Answers

Steven Parker
Steven Parker
230,946 Points

There's an example of the dot notation being used in the Teachers's Notes section at the bottom of the previous video page. The main difference is that example adds two of them in a console.log statement, but here you only need one in an if statement.

I just had a look at the example in the teacher's notes. I'm still confused. Could you simply write out the code?

Steven Parker
Steven Parker
230,946 Points

Give it your best "good faith" try, and if you have trouble, show the code you wrote here and I'll help you with it.

Actually I tried several times before I asked for help. I got nothing but error messages. Here's one approach I tried.

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

Steven Parker
Steven Parker
230,946 Points

You're close! But the code inside an object doesn't refer to itself by name, but with the special keyword "this".

Fix that, and you'll have it.

Thank you for your help.

Steven Parker
Steven Parker
230,946 Points

luisgomez7 — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!

I tried running the below code and it doesn't work. I understand that you're trying to be helpful but not being able to solve this problem is becoming very frustrating for me. Please provide me with the correct answer and, if you like, an explanation of what I was doing wrong to help me fully understand this exercise.

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

Steven Parker
Steven Parker
230,946 Points

You were closer last time, all you need to do to the previous code is replace "player1" with "this" in the "if" expression.