Find the Longest Word in a String... JS

Hi!

im doing the exercise for js but in this case my code is working on my computer not in the page :S

function findLongestWordLength(str) {
  let i = [...str]; 
  let contador = 0; 
  let contadorBase = 0; 
  i.forEach((celda,index) => {
    if(celda === " " || index+1 === i.length){
      if(contador >= contadorBase){
        contadorBase = contador; 
        contador = 0; 
      }else{
      	contador = 0; 
      }
    }else{
      contador++; 
    }
  })
  return contadorBase;
}


console.log(findLongestWordLength("What if we try a super-long word such as otorhinolaryngology "));

this returned 19 but the freecodecamp’s test fail… i try not to see the “solutions” unless i really don’t know what to do… any idea why this hasn’t passed the test?

link:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).


Please post a link to the challenge when asking for help.

1 Like

@Tkmanga Your are not correctly dealing with the case where the longest word in the string is the last word (like in the final test).

thanks ! ill try to use that

mmm in the conditional
if(celda === " " || index+1 === i.length)
if the array ends or is a white space, the 2nd count is replace with the actual count (if the actual count is longer than the 2nd)