1 00:00:00,337 --> 00:00:01,530 Welcome back. 2 00:00:01,530 --> 00:00:04,300 This code challenge was a bit more difficult than the first one 3 00:00:04,300 --> 00:00:07,780 because it involved manipulating your string property a little bit. 4 00:00:07,780 --> 00:00:11,210 The goal was to add a method to the object literal called count words, 5 00:00:11,210 --> 00:00:14,400 that returned the number of words in the string property. 6 00:00:14,400 --> 00:00:17,740 Adding a method to an object literal is a lot like adding a property. 7 00:00:17,740 --> 00:00:19,870 It has the key, which is the name of the method, and 8 00:00:19,870 --> 00:00:22,110 a value, which is the actual method. 9 00:00:22,110 --> 00:00:22,810 So let's add our key. 10 00:00:26,708 --> 00:00:27,870 Then our method declaration. 11 00:00:32,329 --> 00:00:35,750 So how do we get the number of words in a phrase? 12 00:00:35,750 --> 00:00:39,280 Strings in JavaScript have a lot of neat methods you can use on them. 13 00:00:39,280 --> 00:00:40,990 One of them is a method called split. 14 00:00:42,360 --> 00:00:46,750 You call the split method on a string, and pass in a delimiter as an argument. 15 00:00:46,750 --> 00:00:48,280 A delimiter is a character or 16 00:00:48,280 --> 00:00:52,250 a sequence of characters that separates parts of texts or string data. 17 00:00:52,250 --> 00:00:57,900 Many times a delimiter is a comma, like in a CSV, or a comma separated value file. 18 00:00:57,900 --> 00:00:59,730 Oftentimes it can also be a space character. 19 00:01:01,470 --> 00:01:04,150 The split method will separate a string into an array, 20 00:01:04,150 --> 00:01:06,980 with each element separated at the delimiter. 21 00:01:06,980 --> 00:01:10,210 So if we use the split method and pass in a space character, 22 00:01:10,210 --> 00:01:13,610 each word of the string will become an element in the array. 23 00:01:13,610 --> 00:01:14,830 So let's look at that. 24 00:01:14,830 --> 00:01:16,718 Let's create a variable to hold our new array. 25 00:01:23,955 --> 00:01:27,428 Then we'll access the string property using this.string. 26 00:01:27,428 --> 00:01:30,322 Remember, the this keyword is how an object can access properties and 27 00:01:30,322 --> 00:01:31,656 methods that belong to itself. 28 00:01:31,656 --> 00:01:34,791 So then we just call the split method and 29 00:01:34,791 --> 00:01:38,317 pass in the space character as an argument. 30 00:01:43,250 --> 00:01:45,727 Once we have our phrase broken into an array of words, 31 00:01:45,727 --> 00:01:48,039 all we need to do is return the length of the array, 32 00:01:48,039 --> 00:01:50,589 which is now equal to the number of words in the phrase. 33 00:01:57,375 --> 00:02:01,755 So if you wanna see this in real time, let's try to call the split method on 34 00:02:01,755 --> 00:02:05,016 the string property and log the output to the console. 35 00:02:17,404 --> 00:02:19,505 So I'm gonna save the file and jump down to the terminal. 36 00:02:26,183 --> 00:02:29,803 Great, as you can see, the output here is showing an array, and 37 00:02:29,803 --> 00:02:33,800 each word of the phrase is an element inside this array. 38 00:02:33,800 --> 00:02:35,610 Great work, and I'll see you in the next code challenge.