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

Andy Durette
Andy Durette
31,183 Points

Not sure why its not passing

I tested the code outside of the site and even changed out this.isTurn to player1.isTurn however it's not passing.

Anyone who could explain why, and what the proper format should be?

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

        } 
    }
}

2 Answers

Steven Parker
Steven Parker
229,732 Points

The "Bummer!" message included this hint: "Are you using the this keyword?". It's not possible to reference the object by name from inside the object, so you must use "this" instead.

And it's never necessary to compare anything to "true". Anything that might have a value of "true" or "false" can be tested simply by naming it. Now in practical use, the unnecessary comparison will still work and not cause an error; but apparently the challenge doesn't allow it.

Daniel Boisselle
seal-mask
.a{fill-rule:evenodd;}techdegree
Daniel Boisselle
Front End Web Development Techdegree Student 17,438 Points

Ah yes! Thank-you!

Also you very well can access 'player1' within the object in this case. Of course it doesn't make any practical sense in terms of modularity.

Andy Durette
Andy Durette
31,183 Points

Thanks for the help, I did have the this keyword however when it wasn't working I switched to the other way of targeting. Good to have further information on why that is in itself an error to do as well, I won't compare to true in future.

Steven Parker
Steven Parker
229,732 Points

I tried it out and was surprised to find that you can reference "player1" in the method (but you cannot in a property). I was expecting the creation of player1 to occur after the object literal.

Daniel Boisselle
seal-mask
.a{fill-rule:evenodd;}techdegree
Daniel Boisselle
Front End Web Development Techdegree Student 17,438 Points

Hello friend,

This is my first ever response. I would just like to say I renewed my subscription to answer your question!

Fortunately your answer is correct, as there is an issue with the compiler.

Simply change your if expression within the 'play' method to:

  if (this.isTurn) { }

Cheers!

~Dan

Steven Parker
Steven Parker
229,732 Points

I think you meant "isTurn" instead of "isTrue".

Andy Durette
Andy Durette
31,183 Points

Noted, this and the comment by Steven Parker has cleared this up for me, amazingly you also renewed your subscription just to answer so that is appreciated.

Thanks