1 00:00:00,300 --> 00:00:03,380 Let's get some practice with the forEach method. 2 00:00:03,380 --> 00:00:06,730 I've cleared out the examples we wrote in the last video. 3 00:00:06,730 --> 00:00:11,390 Now let's use the forEach method to create a new array of the capitalized version of 4 00:00:11,390 --> 00:00:12,730 these fruits. 5 00:00:12,730 --> 00:00:15,780 First, I'll create a new array, capitalizedFruits. 6 00:00:23,301 --> 00:00:26,630 Now, I'll iterate over the fruits array with forEach. 7 00:00:35,780 --> 00:00:40,480 Inside the call back, fruit will be a string from the fruits array. 8 00:00:40,480 --> 00:00:43,760 We can capitalize that string with toUpperCase. 9 00:00:43,760 --> 00:00:48,160 I'll store the capitalized version of the string in a variable, capitalizedFruit. 10 00:00:59,840 --> 00:01:04,280 Now I'll push the capitalized string into the capitalizedFruits array. 11 00:01:09,710 --> 00:01:14,630 Let's log this capitalizedFruits array out so we can see if the program works or not. 12 00:01:24,141 --> 00:01:27,630 When I run the program, you can see it works. 13 00:01:27,630 --> 00:01:30,770 Cool, I wanna give you a challenge. 14 00:01:30,770 --> 00:01:34,210 I'll erase this console window and 15 00:01:34,210 --> 00:01:39,980 this code And paste in an array of prices. 16 00:01:41,210 --> 00:01:44,200 If you want you can copy this snippet from the teacher's notes and 17 00:01:44,200 --> 00:01:48,692 paste it into your workspace or editor, deleting everything else. 18 00:01:48,692 --> 00:01:53,200 Using forEach, see if you can calculate a total cost by adding this list of 19 00:01:53,200 --> 00:01:56,740 prices up and logging the result to the console. 20 00:01:56,740 --> 00:01:59,250 I've added a comment here next to the array, so 21 00:01:59,250 --> 00:02:02,770 you can see the result your program should display. 22 00:02:02,770 --> 00:02:04,580 Pause the video and give it a try. 23 00:02:06,532 --> 00:02:10,190 Now I'll show you how I solved this challenge. 24 00:02:10,190 --> 00:02:13,020 I knew I'd have to keep track of a total price, so 25 00:02:13,020 --> 00:02:16,364 I first created a variable called total and set it to 0. 26 00:02:20,280 --> 00:02:25,530 Then I wanted to iterate over the prices array using forEach to get each price. 27 00:02:35,560 --> 00:02:40,430 I wanted to add each price to the total and store that value back to total. 28 00:02:44,760 --> 00:02:49,440 Once all the prices had been added together, I logged them to the console. 29 00:02:53,150 --> 00:02:55,170 Running the iteration file. 30 00:02:58,432 --> 00:02:59,830 You can see it works. 31 00:03:01,650 --> 00:03:02,210 Lets try one more. 32 00:03:02,210 --> 00:03:06,275 I'll clear the console and this file. 33 00:03:08,756 --> 00:03:11,670 And then I'll paste in an array of names. 34 00:03:14,120 --> 00:03:17,700 Again this is in the snippet section of the teacher's notes if you'd like to copy 35 00:03:17,700 --> 00:03:19,180 and paste them. 36 00:03:19,180 --> 00:03:23,940 See if you can create a new array with only the names starting with a letter s. 37 00:03:23,940 --> 00:03:24,888 Here's a hint, 38 00:03:24,888 --> 00:03:30,040 you'll need to find some way of checking the first character of each name. 39 00:03:30,040 --> 00:03:33,510 Check the teacher's notes for a link to MDN's page on strings, 40 00:03:33,510 --> 00:03:36,550 if you want to do a little research for this one. 41 00:03:36,550 --> 00:03:40,827 Go ahead and pause the video and see if you can do it. 42 00:03:40,827 --> 00:03:43,090 Here's how I solved this. 43 00:03:43,090 --> 00:03:49,220 I first created a variable named sNames and set it equal to an empty array. 44 00:03:51,449 --> 00:03:54,750 Then I used forEach to iterate over the names array. 45 00:04:04,730 --> 00:04:09,442 I had to check the first letter of each name to know whether I wanted to add it to 46 00:04:09,442 --> 00:04:11,120 my sNames array. 47 00:04:11,120 --> 00:04:14,990 You could do this a couple ways, I used the charAt string method. 48 00:04:20,289 --> 00:04:22,554 Passing in 0. 49 00:04:22,554 --> 00:04:27,219 Because that's the first position of a string. 50 00:04:27,219 --> 00:04:30,960 I checked if this was equal to a capital S. 51 00:04:38,187 --> 00:04:41,930 If it was, I wanted to add the name to my sNames array. 52 00:04:47,220 --> 00:04:51,389 Finally I logged the sNames array to the console, so 53 00:04:51,389 --> 00:04:54,340 I could see if my program succeeded. 54 00:04:59,710 --> 00:05:01,160 I ran the file. 55 00:05:05,810 --> 00:05:07,050 And it works.