Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
See the solution for the previous four game logic methods.
Ternaries
Check out the JavaScript Syntax Reference or the MDN documentation to learn more about ternaries.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back.
0:00
You just tackled four methods on your own.
0:01
Mark, checkForWin,
switchPlayers, and gameOver.
0:03
Most of these were short methods.
0:07
Let's go through the solutions for
each one.
0:09
We'll start with the mark
method in space.js.
0:12
The purpose of this method was to change
a given space object's token property so
0:15
it's set to the token object
that was dropped into it.
0:20
The token object is
passed in as an argument.
0:24
To update the property value,
it's just this.token = token.
0:27
Pretty easy, right, okay.
0:32
Let's move back to game.js.
0:34
The code for an explanation of the
checkForWin method was provided to you.
0:36
You can see my method here,
0:41
but I've got the code collapsed in my
text editor because it's pretty lengthy.
0:42
Make sure you copied the provided
solution into the game.js file or
0:46
checked yours against it.
0:51
Okay, next,
you wrote a method called switchPlayers.
0:53
This method changes the value of
the active property of each player.
0:56
The active player becomes inactive and
vice versa.
1:00
I used a for-of loop to
circle through each player.
1:04
Then, I used a ternary expression to
change the value of the property.
1:07
This part of the expression
indicates a test or condition.
1:12
It's saying, if the player's
active property equals true, then,
1:16
if the condition passes, return false.
1:21
If it fails, return true.
1:24
See the teacher's notes for
more info on ternaries.
1:27
Finally, let's look at
the gameOver method.
1:30
This method is called
when the game is over.
1:33
Its job is to display a game
over message in the browser.
1:35
It receives the message,
a string, as an argument, and
1:39
then updates the game-over HTML element.
1:43
So it's visible, and contains the message.
1:46
Great work, in the next step you'll put
all of these methods together to complete
1:50
our game logic.
1:55
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up