Tell us what’s happening:
Describe your issue in detail here.
I solved the challenge in another method. But i would like to know why this code doesn’t work. The elements of words list is not being passed to word variable in for loop but i don’t know why?
Your code so far
function findLongestWordLength(str) {
let words = str.match(/\w+/ig);
//console.log(words)
let longestWord = "";
for (let word in words) {
console.log(word)
if (word.length > longestWord.length) {
longestWord = word;
}
}
console.log(longestWord.length)
return longestWord.length;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.54
Challenge: Basic Algorithm Scripting - Find the Longest Word in a String
Link to the challenge: