Why can't I get arr=[3,5,53,6,4,3,4,3]

Tell us what’s happening:
Describe your issue in detail here.

   **Your code so far**

function findLongestWordLength(str) {
 let myRegex = /\w+/mig;
 const result = str.match(myRegex);   
// console.log(result);  
 for(let i=0; i<result.length; i++){
   let arr=[];

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/91.0.4472.164 Safari/537.36 OPR/77.0.4054.277

Challenge: Find the Longest Word in a String

Link to the challenge:

Just as a guide.
In this challenge you need to:

  1. create a variable and equal it to zero
  2. split string into an array of separate words
  3. use a loop to iterate over the words
  4. use conditionals (if statement) to check if the length of the each word is greater than your variable, if it is greater you then assign that as the new value of your variable.
  5. return your variable

You’re almost there, the only problem is that in every step of the loop, you reinitialise arr as an empty array.

On a sidenote, there’s an easier way to turn a string of words into an array, you could use the .split() method.

1 Like

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