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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 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,253 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,253 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 :)