How to isolate each word in a sentence string

Tell us what’s happening:
Right now I need to figure out how to separate each of the strings from the whole of the sentence. Right now using the .length function on the string, it counts all the letters of all the words. I figure that identifying the spaces might be useful potentially using the regular expressions or .match functions. Looking for some advice if you have some!

Your code so far


function findLongestWordLength(str) {
let x = [str];
console.log(str.length);
return str.length;
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:

This lesson is a little further down in the curriculum, but could be very helpful here.

1 Like

Creating an array of words from a given string can be accomplished using String.prototype.split(' ').

If you wanted to convert a string containing a few sentences worth of text into an array of characters rather than the above code for an array of words, you would simply use String.prototype.split('').

MDN docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

you can use split(), you can use a regular expression and match() to get an array of words…

You can also do it without creating an array, with just the string. It uses simpler things, but the logic may not be so intuitive.