Basic algorithm scripting_passing in editor with right value but not freecodecamp

Tell us what’s happening:
Not sure why its not passing. I put in VSCode and ran in web and output was as needed.

Your code so far


function findLongestWordLength(str) {
  str = str.split(" ")  
for (i=0; i<(str.length-1) ; i){    
  let wordOne = str[i].split("")
  let wordTwo = str[i+1].split("")
  if (wordOne.length >= wordTwo.length){      
    str.splice(i+1,1)     
  } else {
    str.splice(i,1)
    } }  longNum = str[0].split("")
  return longNum.length } 

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:

Hi and welcome to the forum!

The error in the freeCodeCamp editor says

ReferenceError: assignment to undeclared variable i

Looking back at your code

you never declared i


Side note - the fact that you are not incrementing i is an indication that you are using the wrong sort of control structure for this problem.


function findLongestWordLength(str) {
  str = str.split(" ")  
  for (i=0; i<(str.length-1); i) {    
    let wordOne = str[i].split("")
    let wordTwo = str[i+1].split("")
    if (wordOne.length >= wordTwo.length) {      
      str.splice(i+1, 1)     
    } else {
      str.splice(i, 1)
    }
  }
  longNum = str[0].split("")
  return longNum.length
}

I went ahead and formatted your code to make it easier for others to read, understand, and help.

1 Like

Thank you so much. Do you suggest a “for while” instead?

there is not a “for while” loop, but a while loop or a do…while loop could work for you in this case

1 Like

Thank you and excuse my ignorant statement.