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