1
00:00:01,390 --> 00:00:06,880
In this lesson, the next function that we needed to build is counting the number of words within any

2
00:00:06,880 --> 00:00:12,370
given string in JavaScript has some really nice string functions.

3
00:00:12,850 --> 00:00:19,900
Split allows you to split a string object into an array of strings and we can specify the needle that

4
00:00:19,900 --> 00:00:21,000
we want to split up by.

5
00:00:21,250 --> 00:00:25,210
So we take a look at this example here at the Modular Developer Network.

6
00:00:25,450 --> 00:00:28,990
We've got the string, we've got a string split.

7
00:00:29,140 --> 00:00:30,730
So it's splitting by spaces.

8
00:00:30,880 --> 00:00:34,990
And that means that every one of these spaces means that it's a new word.

9
00:00:35,830 --> 00:00:42,910
And when we split by the spaces, we get an array and the third item, third index item within this

10
00:00:43,180 --> 00:00:45,340
string value is going to be Faulkes.

11
00:00:45,520 --> 00:00:50,320
As we can see, it turned the string into an array, splitting it by the spaces.

12
00:00:50,560 --> 00:00:56,890
And this is also going to give us the ability to count all of those spaces that are available within

13
00:00:56,890 --> 00:00:58,150
any given string.

14
00:00:58,150 --> 00:01:02,080
And we're going to use that as a function to count words.

15
00:01:02,770 --> 00:01:07,150
So we have our string value here and we want to pass it in.

16
00:01:08,080 --> 00:01:14,140
To function, and you could also do it within that same statement as well.

17
00:01:14,890 --> 00:01:22,210
Create a temporary variable where we're taking that value that got passed in the string words, we're

18
00:01:22,210 --> 00:01:29,380
using the same method that we saw within the string prototype where we can split, how we're splitting

19
00:01:29,380 --> 00:01:30,820
it by space.

20
00:01:30,820 --> 00:01:36,160
And then we want to return back the length because now it's going to be an array and we can console

21
00:01:36,160 --> 00:01:40,800
log out that response and we simply want to return back that response.

22
00:01:41,080 --> 00:01:44,670
So let's refresh past something into word counter.

23
00:01:44,980 --> 00:01:49,370
So we've got Hello World and we can see that two gets returned back.

24
00:01:49,600 --> 00:01:52,210
So we've got two words in there, which is true.

25
00:01:52,360 --> 00:01:57,130
We've got a response back of two and we also outputting that within the console.

26
00:01:58,030 --> 00:02:04,720
So now we know we have a way to count the number of words and we're going to pass that string into word

27
00:02:05,290 --> 00:02:11,040
counter, so that's the function word counter and return back the number of words.

28
00:02:11,260 --> 00:02:17,830
And now we can use that within a formula where we can calculate out the speed, the number of words

29
00:02:17,830 --> 00:02:22,200
per minute, and so we can use that math function again.

30
00:02:22,510 --> 00:02:24,070
We're going to math around.

31
00:02:24,250 --> 00:02:32,110
We're going to take our word count value that we just got, divide it by the total time and multiply

32
00:02:32,110 --> 00:02:33,460
it by 60.

33
00:02:33,670 --> 00:02:38,860
So we get a count of minutes because remember, again, that the total time is seconds.

34
00:02:39,040 --> 00:02:43,350
So we have to multiply it by 60 to get the number of words per minute.

35
00:02:44,350 --> 00:02:46,780
So let's console log out that value.

36
00:02:47,080 --> 00:02:49,990
And this is going to be the number of words that we typed per minute.

37
00:02:51,160 --> 00:02:52,780
So do a test, test, test.

38
00:02:54,240 --> 00:03:00,990
And as you can see, we're typing 191 words per minute and let's create another variable here, we can

39
00:03:00,990 --> 00:03:07,410
call this final message and in here we can say this is where you can use the variable speed or we're

40
00:03:07,410 --> 00:03:15,300
taking the message element, updating the inner HTML of it with whatever value is contained within final

41
00:03:15,300 --> 00:03:15,750
message.

42
00:03:16,200 --> 00:03:19,410
We type some words and I know that it's not the actual words that are there.

43
00:03:19,710 --> 00:03:25,110
We see that you typed 102 words per minute and we didn't get any of them right.

44
00:03:25,230 --> 00:03:31,800
So we need one more thing here in order to check to see if the player actually typed the words correctly.

45
00:03:32,070 --> 00:03:32,880
So that's still to come.
