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 Practice forEach in JavaScript!
You have completed Practice forEach in JavaScript!
Preview
Let's walkthrough the solution to the sixth code challenge.
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
For this challenge,
0:00
we need to use the forEach method to
iterate over the colors array and
0:01
store the hex colors that start with
the letter F in the array, filteredColors.
0:05
As we've done with each of the other
challenges I'll start with copying and
0:11
pasting code into Atom in order
to make it easier to read.
0:14
So here's another challenge that requires
us to evaluate each value in order to
0:23
determine if it should be added
to the filteredColors array.
0:27
To start, I stubbed out my solution by
adding a call to the forEach method.
0:32
So colors.forEach.
0:37
I used color for
the current value parameter name,
0:41
and a fat arrow and
a set curly braces to define a code block.
0:43
Then, I added an if statement.
0:49
Check the second character in the hex
color, starts with a letter F.
0:51
So if, parenthesis, curly braces.
0:55
To get the second character for the hex
color, I use the CharAt string method.
1:00
So color.charAt.
1:05
The argument that you
passed the charAt method
1:08
is the index of the character
that you want to retrieve.
1:10
We want the second character,
so I'll pass an index of one.
1:14
And then we want the check to see
if the character is equal to F.
1:20
Specifically a capital F.
1:24
Alternatively to get
the character at an index of one,
1:27
you could just index into the string,
like this.
1:31
Let's finish this up.
1:39
If the if statement evaluates to true,
1:40
we want to push the current color
onto the filteredColors array.
1:42
And that's the complete solution.
1:51
Great job, just one more challenge to go.
1:53
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