Basic Algorithmic Scripting: Find Longest Word in String (Simple AF) Solution

Tell us what’s happening:
Solution without .map() .split() methods that were not introduced in earlier topics/challenges. Following code uses whatever’s been taught up to this point.

  **Your code so far**

function findLongestWordLength(str) {

let regEx = /\w+(')?\w+/ig;

let wordsArr = str.match(regEx);  //returns an array

let lengthOfEachWordArr = [];

for(let word of wordsArr){

lengthOfEachWordArr.push(word.length);

}

let longestWordLength = Math.max(...lengthOfEachWordArr);

return longestWordLength;

}

findLongestWordLength("What is the average airspeed velocity of an unladen swallow");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering forum posters question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Note that there are several example solutions for this challenge under the “get a hint” link, and that the first of those uses nothing except loop statements

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.