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
Now that you know how the forEach method works, let's get some more practice with it.
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
Let's get some practice
with the forEach method.
0:00
I've cleared out the examples
we wrote in the last video.
0:03
Now let's use the forEach method to create
a new array of the capitalized version of
0:06
these fruits.
0:11
First, I'll create a new array,
capitalizedFruits.
0:12
Now, I'll iterate over
the fruits array with forEach.
0:23
Inside the call back, fruit will
be a string from the fruits array.
0:35
We can capitalize that
string with toUpperCase.
0:40
I'll store the capitalized version of the
string in a variable, capitalizedFruit.
0:43
Now I'll push the capitalized string
into the capitalizedFruits array.
0:59
Let's log this capitalizedFruits array out
so we can see if the program works or not.
1:09
When I run the program,
you can see it works.
1:24
Cool, I wanna give you a challenge.
1:27
I'll erase this console window and
1:30
this code And paste in an array of prices.
1:34
If you want you can copy this
snippet from the teacher's notes and
1:41
paste it into your workspace or
editor, deleting everything else.
1:44
Using forEach, see if you can calculate
a total cost by adding this list of
1:48
prices up and
logging the result to the console.
1:53
I've added a comment here
next to the array, so
1:56
you can see the result your
program should display.
1:59
Pause the video and give it a try.
2:02
Now I'll show you how I
solved this challenge.
2:06
I knew I'd have to keep
track of a total price, so
2:10
I first created a variable
called total and set it to 0.
2:13
Then I wanted to iterate over the prices
array using forEach to get each price.
2:20
I wanted to add each price to the total
and store that value back to total.
2:35
Once all the prices had been added
together, I logged them to the console.
2:44
Running the iteration file.
2:53
You can see it works.
2:58
Lets try one more.
3:01
I'll clear the console and this file.
3:02
And then I'll paste in an array of names.
3:08
Again this is in the snippet section of
the teacher's notes if you'd like to copy
3:14
and paste them.
3:17
See if you can create a new array with
only the names starting with a letter s.
3:19
Here's a hint,
3:23
you'll need to find some way of checking
the first character of each name.
3:24
Check the teacher's notes for
a link to MDN's page on strings,
3:30
if you want to do a little research for
this one.
3:33
Go ahead and pause the video and
see if you can do it.
3:36
Here's how I solved this.
3:40
I first created a variable named sNames
and set it equal to an empty array.
3:43
Then I used forEach to
iterate over the names array.
3:51
I had to check the first letter of each
name to know whether I wanted to add it to
4:04
my sNames array.
4:09
You could do this a couple ways,
I used the charAt string method.
4:11
Passing in 0.
4:20
Because that's the first
position of a string.
4:22
I checked if this was
equal to a capital S.
4:27
If it was, I wanted to add
the name to my sNames array.
4:38
Finally I logged the sNames
array to the console, so
4:47
I could see if my program succeeded.
4:51
I ran the file.
4:59
And it works.
5:05
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