In this lesson, the next function that we needed to build is counting the number of words within any
given string in JavaScript has some really nice string functions.
Split allows you to split a string object into an array of strings and we can specify the needle that
we want to split up by.
So we take a look at this example here at the Modular Developer Network.
We've got the string, we've got a string split.
So it's splitting by spaces.
And that means that every one of these spaces means that it's a new word.
And when we split by the spaces, we get an array and the third item, third index item within this
string value is going to be Faulkes.
As we can see, it turned the string into an array, splitting it by the spaces.
And this is also going to give us the ability to count all of those spaces that are available within
any given string.
And we're going to use that as a function to count words.
So we have our string value here and we want to pass it in.
To function, and you could also do it within that same statement as well.
Create a temporary variable where we're taking that value that got passed in the string words, we're
using the same method that we saw within the string prototype where we can split, how we're splitting
it by space.
And then we want to return back the length because now it's going to be an array and we can console
log out that response and we simply want to return back that response.
So let's refresh past something into word counter.
So we've got Hello World and we can see that two gets returned back.
So we've got two words in there, which is true.
We've got a response back of two and we also outputting that within the console.
So now we know we have a way to count the number of words and we're going to pass that string into word
counter, so that's the function word counter and return back the number of words.
And now we can use that within a formula where we can calculate out the speed, the number of words
per minute, and so we can use that math function again.
We're going to math around.
We're going to take our word count value that we just got, divide it by the total time and multiply
it by 60.
So we get a count of minutes because remember, again, that the total time is seconds.
So we have to multiply it by 60 to get the number of words per minute.
So let's console log out that value.
And this is going to be the number of words that we typed per minute.
So do a test, test, test.
And as you can see, we're typing 191 words per minute and let's create another variable here, we can
call this final message and in here we can say this is where you can use the variable speed or we're
taking the message element, updating the inner HTML of it with whatever value is contained within final
message.
We type some words and I know that it's not the actual words that are there.
We see that you typed 102 words per minute and we didn't get any of them right.
So we need one more thing here in order to check to see if the player actually typed the words correctly.
So that's still to come.