I am working on a task to search any given string for the longest word. I am thinking the ideal way to do this is to split the string into an array consisting of each word then looping through that array and comparing each length to return the longest. I am stuck on just separating the string into an array of words. The questions I have seen answered similar in topic seem to use much to complicated methods for such a simple task. I am looking for help on a beginner level as this is my first time incorporating regular expressions into my code.
function findLongestWord(str) {
str = str.toLowerCase().split("/\w+");
return str;
}
findLongestWord('The quick brown fox jumped over the lazy dog');
Aucun commentaire:
Enregistrer un commentaire