Tell us what’s happening:
typed the soln its not working
Your code so far
function findLongestWordLength(str) {
var words=str.split('');
var maxLength=0;
for (var i=0; i<words.length;i++) {
if (words[i].length>maxLength) {
maxLength=words[i].length;
}
}
return maxLength;
}
function findLongestWordLength(str) {
var words=str.split('');
var maxLength=0;
for (var i=0; i<words.length;i++) {
if (words[i].length>maxLength) {
maxLength=words[i].length;
}
}
return maxLength;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.
What is your result? It’s difficult to find the error if we don’t know what happens whit your code. As said @ILM, it’s a good idea place some console messages to view what happens whit the variables trought the whole algoritm.
At first sight, i think may be a mistake in your “split” function. You used a empty string as words separator, and must be an space: ’ ', instead ‘’.
Of course.
When you use a function “split”; what you do is to separate a string in diferente pieces, acording to a specific parameter. For what you are looking for, you need to separate the entire string on words. For that, you must a space (" “) as separator. Then, the string will be cutted every time it find a space (between words), and will push the fragment into an array (your “words” variable).
If you use an empty string (”") instead a space (" "), the “split” function will separate not words, but characters.