How can I get different numbers?

Tell us what’s happening:

I can understand how to get a number but how can I get a different number for different strings?

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;
}

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

Your browser information:

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

Challenge: Find the Longest Word in a String

Link to the challenge:

Hello @maeforest :wave:,

I don’t really get what your question is asking, but assuming that you mean every string gives out different numbers meaning the string length. If you do "The quick brown fox jumped over the lazy dog".length; it will return 44, because there is 44 characters inside the string. In your function, you are splitting the sentence into an array and looping through it. So there will be different numbers because each word have different lengths.

Hope this answers your question. :slightly_smiling_face:

That’s what I want. First I want to count the letters in each word and then find the longest word. That’s why I have to split it first.

It’s literally what you’re doing though, I don’t understand what you’re asking: in your code you are splitting the string then counting the letters in each word in turn

Hi maeforest,

maybe check again this line. Words are separated by spaces.

var words = str.split("");