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 trialJaco Kotzee
Courses Plus Student 20,157 PointsStuck on quiz but I think my code is correct.
Stuck on quiz but I think my code is correct. Is this a bug?
const player1 = {
name: 'Ashley',
color: 'purple',
isTurn: true,
play: function() {
if (player1.isTurn === true) {}
}
}
1 Answer
andren
28,558 PointsYour code would technically work, but it is not ideal for two reasons:
- It is bad form to reference the name of the variable the object is stored inside of inside of the object. This is due to the fact that while the code would work in this specific case, if the object was assigned to a variable or parameter with a different name then bugs could occur.
-
isTurn
contains a Boolean so comparing it totrue
is pointless. The comparison will result in the exact same value already stored in the variable.
The first issue can be solved by using the this
keyword, and the second by just not using a comparison at all. Like this:
const player1 = {
name: 'Ashley',
color: 'purple',
isTurn: true,
play: function() {
if (this.isTurn) {}
}
}
With those changes your code will be accepted by the code checker.
Jaco Kotzee
Courses Plus Student 20,157 PointsJaco Kotzee
Courses Plus Student 20,157 PointsThank you andren!