Basic Algorithm Scripting - Find the Longest Word in a String

**So basically it ignores a word **
when i run the code with this sentence it stops at the index of ‘super-long’.

function findLongestWordLength(str) {
let mewStr = ;
let yourMom = ‘’
let biggestWord = ‘’;
for(let index = 0 ; index < str.length;index++){
if(str[index] !== ’ '){
yourMom += str[index];
}else{
mewStr.push(yourMom);
yourMom = ‘’
}
}
for(let i = 0 ; i < mewStr.length;i++){
if (mewStr[i].length > biggestWord.length){
biggestWord = mewStr[i];
}
}
return biggestWord;
}

console.log(findLongestWordLength(“What if we try a super-long word such as otorhinolaryngology”))**

function findLongestWordLength(str) {
  let mewStr = [];
  let yourMom = ''
  let biggestWord = '';
  for(let index = 0 ; index  < str.length;index++){
if(str[index] !== ' '){
  yourMom += str[index];
}else{
  mewStr.push(yourMom);
  yourMom = ''
}
  }  
  for(let i = 0 ; i < newStr.length;i++){
if (mewStr[i].length > biggestWord.length){
  biggestWord = newStr[i];
}
  }
 
  return biggestWord.length;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Find the Longest Word in a String

*Link to the challenge:

if you delete the semi-colon at the start of this line you will see the following error
in the console:

ReferenceError: newStr is not defined

Hopefully you can fix this and go from there. (or let us know if you need further help)

also this if statement only pushes a word when it finds a space.
So obviously it will ignore the last word since there is no space after it?

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