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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsCheck 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.
const player1 = {
name: 'Ashley',
color: 'purple',
isTurn: true,
play: function(){
// write code here.
if( player1.isTurn ) {
}
}
}
2 Answers
Matthew Lang
13,483 PointsI 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!";
}
}
}
Adam Beer
11,314 PointsHi 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
Treehouse Moderator 91,253 PointsThanks 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 :)
Adam Beer
11,314 PointsPlease chech this short course :) (https://teamtreehouse.com/library/understanding-this-in-javascript)
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsUnless 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