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 trialSumalee Juntra
8,669 PointsHow to use the dot notation in this task?
Inside the play method, write an empty if statement that checks if it's the players turn. Use dot notation.
const player1 = {
name: 'Ashley',
color: 'purple',
isTurn: true,
play: function(){
}
}
5 Answers
realedwardleung
7,197 Pointsconst player1 = { name: 'Ashley', color: 'purple', isTurn: true, play: function(){ if (this.isTurn) { return player1['name'] + ' is now playing!' } } }
Sumalee Juntra
8,669 PointsLike this!
const player1 = { name: 'Ashley', color: 'purple', isTurn: true, play: function(){ if(true) { return player1.name; } } }
Zimri Leijen
11,835 PointsAh, that if statement is always going to be true.
You will want a statement that is only true when it's player1's turn.
Sumalee Juntra
8,669 PointsIt's doesn't work like this, too.
const player1 = { name: 'Ashley', color: 'purple', isTurn: true, play: function(){ if(isTurn) { return player1.name; } } }
Danny L.
10,837 PointsHi Sumalee,
I know this post is a few months only but I figured I'd answer anyway.
Your code is almost there but it is missing the this keyword to specify where the isTurn is coming from. It should look like this:
const player1 = {
name: 'Ashley',
color: 'purple',
isTurn: true,
play: function () {
//below is where you need to include the this keyword
if(this.isTurn) {
return player1.name + " is now playing!"
}
}
}
Hope that helps!
realedwardleung
7,197 PointsYou're welcome, happy coding :)
Zimri Leijen
11,835 Pointsplayer1
is an object.
To access a property in an object, you can use dot notation or bracket notation.
For example, if I wanted the name, I could type either player1.name
(dot notation) or player1['name']
(bracket notation, note that it's a string within the brackets, this is because with bracket notation you can use variables and numbers as well, in fact ANY type of expression is allowed within brackets, even functions! Whereas dot notation ONLY allows strings)
Sumalee Juntra
8,669 PointsThank you so much for your help, but I still can't pass this task. :(
Zimri Leijen
11,835 Pointswhat did you try?
Sumalee Juntra
8,669 PointsSumalee Juntra
8,669 PointsThank you so much. :)
Mel Rumsey
Treehouse ModeratorMel Rumsey
Treehouse ModeratorHey thanks for the help on this it worked. But, I am kind of confused where the 'this' came from though. I don't remember anything about adding a 'this' before a dot notation.
Mica Phan
12,241 PointsMica Phan
12,241 PointsExplanation for the 'this' keyword is mentioned in the Teachers Notes.