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 trialluisgomez7
5,557 PointsI don't know how to use Java script dot notation with an if statement. How do I solve this problem?
const player1 = { name: 'Ashley', color: 'purple', isTurn: true, play: function(){ // write code here. (I know the If statement goes here but I don't know how to combine it with java script notation. } }
const player1 = {
name: 'Ashley',
color: 'purple',
isTurn: true,
play: function(){
// write code here.
}
}
4 Answers
Steven Parker
231,248 PointsThere's an example of the dot notation being used in the Teachers's Notes section at the bottom of the previous video page. The main difference is that example adds two of them in a console.log
statement, but here you only need one in an if
statement.
luisgomez7
5,557 PointsActually I tried several times before I asked for help. I got nothing but error messages. Here's one approach I tried.
const player1 = { name: 'Ashley', color: 'purple', isTurn: true, play: function(){ if(player1.isTurn); } }
Steven Parker
231,248 PointsYou're close! But the code inside an object doesn't refer to itself by name, but with the special keyword "this".
Fix that, and you'll have it.
luisgomez7
5,557 PointsThank you for your help.
Steven Parker
231,248 Pointsluisgomez7 — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!
luisgomez7
5,557 PointsI tried running the below code and it doesn't work. I understand that you're trying to be helpful but not being able to solve this problem is becoming very frustrating for me. Please provide me with the correct answer and, if you like, an explanation of what I was doing wrong to help me fully understand this exercise.
const player1 = { name: 'Ashley', color: 'purple', isTurn: true, play: function(){ if (this.player1.iTurn = true); } }
Steven Parker
231,248 PointsYou were closer last time, all you need to do to the previous code is replace "player1" with "this" in the "if" expression.
luisgomez7
5,557 Pointsluisgomez7
5,557 PointsI just had a look at the example in the teacher's notes. I'm still confused. Could you simply write out the code?
Steven Parker
231,248 PointsSteven Parker
231,248 PointsGive it your best "good faith" try, and if you have trouble, show the code you wrote here and I'll help you with it.