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 trialAndrea Zajicova
7,124 PointsswitchPlayers() method solution: Is this also possible?
I wrote the switchPlayers() method like this:
switchPlayers(){ for (let player of this.players) { if (player.active) { this.active = true; } else if (!player.active) { this.active; } } }
Is it also possible? Thank you. I appreciate immensely any potential answer. I am learning lots from them. Happy coding and debugging! :)
Steven Parker
231,248 PointsMark Casavantes — this snippet is intended to be part of a class definition and would cause a syntax error only if used by itself.
5 Answers
Steven Parker
231,248 PointsThis does something entirely different. The original method changed the "active" property of each player, this one would not change the players but create a new "active" property directly on the Game object.
Did you test it out in the project code to see if it worked for yourself?
Steven Parker
231,248 PointsThere are, however, plenty ways to code this and the original always struck me as rather verbose. I'd probably do something like this:
switchPlayers() {
for (let player of this.players) {
player.active = !player.active;
}
}
Andrea Zajicova
7,124 PointsThank you, Mark and Steven. I tested it, and it didn't work, but an explanation of why always helps me.
Thank you so much for taking the time to reply to my question :)
Mark Casavantes
10,619 PointsSteven Parker is an expert. I learn from his answers. Thank you.
Jason Larson
8,361 PointsI agree with Steven (and Mark), and that is in fact how I wrote the method into my code. Whenever possible, it's more efficient to tell the code to do something, rather than first having it check on whether or not it needs to do it. When you are just toggling the state of a boolean, remember that the not operator ( ! ) gives back its opposite state. In fact, it wouldn't surprise me if there was a change at some point in the future to allow you to just use an exclamation at the end of a boolean to change its value, much like the increment and decrement operators ( ++ and --).
wildgoosestudent
11,274 PointsI used the ternary operator, but a bit more concise. The player.active?
parts inherently checks if it is true rather than writing it out like Ashley did.
switchPlayers() {
for (let player of this.players) {
player.active = player.active? false : true;
}
}
Mark Casavantes
10,619 PointsMark Casavantes
10,619 PointsI do not think so. I put your information in an IDE and broke up your work on different lines. I got an error on switchPlayers () {. Uncaught SyntaxError: Unexpected token '{' (sketch: line 1). I am a rookie. I try to help solve questions other students have. I do not think this is a complete answer, but it is the best I can do.