Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Object-Oriented JavaScript: Challenge!
You have completed Object-Oriented JavaScript: Challenge!
Preview
See the solution for the unusedTokens, activePlayer, and activeToken getter methods.
This video doesn't have any notes.
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
Okay, let's dive right in.
0:00
This video will go over solutions for
the following get a methods.
0:01
Unused tokens, active token and
active player.
0:04
Open up the player.js file.
0:08
We'll start with unused tokens.
0:10
There's a handy JavaScript
array method called filter,
0:12
that I've used to write a quick and
succinct getter method for unused tokens.
0:16
The filter method is called on
an array and it's passed the test.
0:20
The method returns in
the form of another array,
0:24
any elements of the original array
that pass the provided test.
0:27
So you've called the filter method
on the player's token's property,
0:32
which is holding an array of every
token object belonging to the player.
0:35
I passed in the test.
0:39
What you see here is
JavaScript shorthand for
0:41
any token object whose dropped
property does not equal true.
0:44
You could have also used a regular for
loop, a for of loop or
0:49
a foreach to find the non dropped
tokens and push them onto an array.
0:53
Great work, let's move on to
the activeToken getter method.
0:58
This getter method returns
the first token object inside
1:02
the unusedTokens array you just created.
1:06
Every time a player takes a turn,
1:09
this getter method grabs the first
token object in this array.
1:11
Later, you'll see that when this token is
dropped into the game board, its dropped
1:15
property will be set to true, thus
removing it from the unused tokens array.
1:20
As you can see, the solution for
this getter method is really simple.
1:24
Because the unused tokens array
was created in the getter method,
1:28
it can be access like
any regular property.
1:32
We find the first element in the array as
denoted by the brackets with the index
1:34
number zero inside and simply return it.
1:39
Okay, let's jump over to game.js and
1:42
look at the last getter method in
your assignment, activePlayer.
1:44
The solution for this getter method
is a lot like the solution for
1:48
the unused tokens getter method.
1:52
For unused tokens we used array method
filter, that method returns an array.
1:55
Because we're only looking for
a single value this time,
2:00
we can use array method find instead.
2:04
Find is a lot like filter except instead
of returning a new array of every
2:06
element that passes the provided test,
find returns the first element or
2:11
in our case,
the only element that passes the test.
2:16
Our test condition for
2:19
this gather method is does the player
objects active property equal true?
2:20
Is this the active player?
2:25
We call this method on the game objects
player's property which hoolds an array
2:27
of the game's players.
2:32
Okay, awesome job.
2:33
We're ready now to fill
out the start game method.
2:35
Head to the next step and
then join me to see the solution and
2:37
take a look at your amazing
progress loaded up on screen.
2:41
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