This is going to be a quick bonus lesson, how to make the scheme even better.
So currently we have a game, we hit start, we can type some content and we see we type 31 words per
minute and we see there were some errors with the wording.
And what I want to do in this lesson is actually count how many words were incorrect.
And the way that I'm going to do that is I'm going to take this value.
I'm going to take the value from here, both of those string values.
I'm going to use that split from the string object, turn them into arrays and then loop through the
contents of the array.
Do comparison word by word.
So let's set up that function, string one and string two, and we're going to do a comparison of both
of them.
So let's create that first split and we're going to split it by spaces.
Let words to equal string two split by space.
And now we're going to loop through one of the strings.
Let's just take words.
One, we're going to loop through each one of those and we can use the for each or we could use any
type of loop that we want that's going to iterate through each one of the items.
Then within here function, we've got our item and for now we can console lock out the item and we're
going to launch the function, compare words so we know we've got message, text one and we've got our
string that we're going to pass in there.
So let's see what happens.
So refresh hit start test done.
And we can see that we broke up all of the words here within our string.
So source code is included.
You can your own version of this challenge so we can try that again.
And we can see once again we get all of the words out there.
So now that we're looping through them, we need to compare each one of those and we need to also get
an index value here.
So let's output the index and refresh and we can get the index using for each as well.
And the index is what we can use for the other array where we've got words to.
So let's refresh.
We see that those are coming up undefined because we're not actually typing any of those words.
So even if I type a few of the first ones, we can see that the first ones do match and then the rest
don't match.
So that's exactly what we want to do.
So we need to do a comparison here to check to see if they are correct.
And if they are correct, then we're going to continue.
And if they're not correct, then we're going to return back that they weren't that they were incorrect.
So having our condition, we're going to check to see if item is equal to words to index.
And if it is, then we know that we've got the count plus one.
Let's set up count here.
So now count will increment every time they are correct and nothing will happen every time they are
wrong.
And now we can console log out and that will show us how many words were correct.
So once again, we'll do start.
Hope you.
So we should have two.
Correct.
Which we do.
And then we can do out of.
And this can actually count how many words we had within our words.
One start again.
We'll try that again.
So hope you are having fun.
We see that to correct out of thirteen.
So this is what we can return back.
So let's return that back out.
And instead of outputting that, we're going to simply add to the final message, whatever gets returned
back from this function.
And we can get rid of this part and we can get rid of that part and just clean that up one more time.
So now we're actually going to get a count of how many words we got.
Correct.
So we got to correct out of nine.
And we can add in words as well there within that string.
So start one more time.
So see how many we got?
We got five out of thirteen.
Correct.
So this gives us a little bit more of an accurate response instead of just saying that we were right
or wrong.
So when I do start and if I just hit done, I got zero correct out of nine words.
And as well you can add additional customizations to this to improve this application as well.