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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,065 Points

Check to see if it is a players "turn" with an empty if statement

Here's my attempt, but I'm a little confused. I'm using dot notation to reference the isTurn property which is "true" automatically so I should be able to get to the value without explicitly identifying it in the condition. If I remember rightly conditions by default check for truthy values.

I'm definitely writing my code inside the method and not using the this keyword which the course doesn't mention yet.

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

        }
    }
}

2 Answers

Matthew Lang
Matthew Lang
13,483 Points

I haven't taken this course. I believe there must be an error in the course structure if it hasn't mentioned this yet, as you need to use the this keyword to complete this challenge.

Here is my code, that passed the challenge:

const player1 = {
    name: 'Ashley',
    color: 'purple',
    isTurn: true,
    play: function(){
        // write code here.
        if(this.isTurn) { 
          return this.name + " is now playing!";
        }
    }
}
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,065 Points

Unless I'm very much mistaken, or not paying enough attention, the this keyword has never been mentioned in a video up to this point or in the teachers notes. Have we missed a video we were meant to see in the course? Ashley Boucher

Adam Beer
Adam Beer
11,314 Points

Hi Jonathan. Inside the if statement use "this" keyword. Bellow the player1 you call the player1.player()(function name). Like this.

var player1 = {
    name: 'Ashley',
    color: 'purple',
    isTurn: true,
    play: function(){
        // write code here.
      if (this.isTurn) {

      }
    }
}
player1.play();
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,065 Points

Thanks Adam, clearly that's the way to do it, just trying to clear up confusion on if students were introduced to that. Noone would be aware of the this keyword if completely new to OOP JavaScript! :-)

I certainly didn't think to use it but then that's more to do with my brain than lack of experience :)