Basic Algorithm Scripting - Find the Longest Word in a String

Tell us what’s happening:
My code works fine but doesn’t match any of the solutions. I’m unsure if my code makes sense or is acceptable. It’d be great if someone could advise. Thanks!

Your code so far

function findLongestWordLength(str) {
  //split the string to array
  let arr = str.split(" ");
  //create an array to store the length of each word
  let arrNum = [];
  for(let i=arr.length-1; i >= 0; i--){
  arrNum.push(arr[i].length);
  }
  //return the longest word
  return Math.max(...arrNum)
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15

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

Link to the challenge:

Yes, your code makes sense and is acceptable. There is almost always more than one way to solve a problem.

Having said that, there are ways you could make this more concise. For example, you are manually looping through arr and creating a second array (arrNum) with just the lengths of the words:

let arrNum = [];
  for(let i=arr.length-1; i >= 0; i--){
  arrNum.push(arr[i].length);
}

These four lines can be condensed into one pretty simple functional method. So the first line could be changed to:

let arrNum = str.split(" ").method(...);

Where you would replace method(...) with the proper array method that would create an array of the word lengths. Now your function would be just two lines of code and I don’t think it would be any less clear what you are doing.

Hi,
Thanks a lot for your advice! :slight_smile:
I couldn’t figure out what method I could use. My guess is map() - but I couldn’t get it right. Or is it forEach?

function findLongestWordLength(str) {

let arr = str.split(" ");
let arrNum = ;
arr.forEach(function (item, index) {
arrNum.push(item.length);
});
return Math.max(…arrNum);
}
findLongestWordLength(“The quick brown fox jumped over the lazy dog”)
;

Ya, I think this is a good guess.

Remember, you can replace those four lines of code with just the map method and tack it onto the end of the first line:

let arrNum = str.split(" ").map(...);

Your job is to figure out what is passed into map.

Thanks! I might have figured it out. Please let me know if it looks more correct lol

function findLongestWordLength(str) {
//split the string to array
// let arr = str.split(" ");

let arrNum = str.split(" ").map(function(value){
return value.length;
});
return Math.max(…arrNum)
}

console.log(findLongestWordLength(“The quick brown fox jumped over the lazy dog”));

I wouldn’t necessarily call it “more correct”. Your original solution worked just fine. I would say that experienced JS programmers would probably prefer this more concise method and thus it would be the “preferred” method. And you could take it even further and combine everything into one return statement, although I might argue that it may be more readable keeping it as two separate lines.

But I will point out that most JS programmers would use the arrow function format instead of the tradition function format for the function passed into map. So I would definitely change that.

Hey everyone!

I’m trying to understand the #1 solution the Challenge Guide gives to this, but I’m having a hard time.

The #1 solution is:

function findLongestWordLength(str) {
let longestLength = 0;
let currentLength = 0;

for (let i = 0; i < str.length; i++) {
  if (str[i] === " ") {
    if (currentLength > longestLength) {
      longestLength = currentLength;
    }
    currentLength = 0;
  } else {
    currentLength++;
  }
}
if (currentLength > longestLength) {
  longestLength = currentLength;
}

return longestLength;
}

I don’t get why the following part of the code is added twice, after the first “if” statement and right before the return statement:

if (currentLength > longestLength) {
  longestLength = currentLength;
}

Could someone please give me a hand?

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