Find the Longest Word in a String: Problems with Array Transformation?

Tell us what’s happening:

I’m not passing this test.
What I think that I’m doing is the following:

  1. creating a variable to store transformation of str to array
  2. reassigning str using let as well as calling the largest length in the array

One problem I’m stuck on is that when I transform str to an array I am breaking down the sentence into individual letters instead of words. This I recognize as my main problem, but perhaps there’s something much bigger…

Your code so far


function findLongestWordLength(str) {
  var arr = str.split("", str.length);
  console.log(arr);
  let str = Math.max(arr);
  return str.length;
}

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/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string

what happens if you put a space between the double quotes in that second line of code?

@DZamora54 is right on about that " " space thing in split. I’m not sure about that second parameter either.

Also, I don’t think this is doing what you think

Redeclaring str won’t work with let - probably should just create a new variable
Math.max is looking for a list of of numbers - you are passing it an array of strings.

Here is what I have so far.

First, I’ll say what I’m trying to do, then what I’m concerned about and where I think I’m stuck.

  1. I split the str array into elements, then sorted those from smallest to largest. Once I did that, I assigned the arr with an index of the last number in the list (which I think is organized from smallest to largest) and returned that.

2.I’m concerned that I may be missing a method or writing the sort method incorrectly OR may be assuming something incorrect about the method I am performing on the array.

  1. I’m stuck because I’m passing no tesrs and I think I should be passing at least the first one, yes? So I’m unsure where I’m messing up. Please help!

function findLongestWordLength(str) {
  var arr = str.split(" ");
  arr.sort(function(a,b){
    b - a;
  });
  return str = arr[-1];
}

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

You can try modifying your sorting function to compare lengths of items instead of raw items. And don’t forget about return.

I’ll answer with some questions

function findLongestWordLength(str) {
  var arr = str.split(" "); 
console.log(arr);

  // maybe not doing what you think 
  // what is the result of  'quick' - 'The' ?
  // what kind of result is sort looking for?
  arr.sort(function(a,b){
    b - a;  // no return?!
  });
console.log(arr);


  // can you use a negative index on an array?
  // how else might you indicate the last index of arr
  // is returning the last string in the array an answer?
console.log(arr[-1]);
  return str = arr[-1];
}


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

Have you considered another method other than sorting? How else could you locate the longest string?

You might benefit from logging variables to the console to see their values as your code progresses. If you are not sure how to do that in your browser, online editors are also handy for that. Here’s your code on repl.it to play with

Here is what I have so far. I have my process listed below, numbered. The issue I think I’m struggling with is number 5 right now. If I could figure if I’m doing that or if I’m almost doing it or if I’m completing missing the mark on this aspect of the algorithm then I think this would help figure out how to proceed from here.


function findLongestWordLength(str) {
 var spl = [];
 var lgth = 0;
 var largest = [];
  spl = str.split(" ");
for(var i=0;i<lgth.length;i++) {
  if(lgth[i].length > i) {
    largest = lgth[i]
    console.log(largest);
  }
}


  return largest.length;
}

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

//1. create function that runs string and returns the string's length
//2. create var that transforms string into array
//3. create var that stores largest unit of string array
//4. separate string array into elements
//5. find element with largest length
//6. transform this length into numerical amount
//7. return numerical amount


I made a few adjustments, but I’m still not getting any further. Where am I going wrong?


function findLongestWordLength(str) {
 var largest = [];
 var longest = str[0];
 if (str.length < 1){
   return "";
 }
 largest = str.split(" ");
 console.log(largest);

for(var i=0;i<largest.length;i++) {
  if(largest[i].length > i) {
  largest = str[i];
   
  }
}


  return largest.length;
}

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

I’ve redone the code a bit. The loop is flawed (still flawed) because it loops through the words rather than accounting for the letters that make them up. I think this has to do with some pre-conditional work I should do (make another storage variable and apply some sort of method there to obtain individual word length. Is the case? Am I on the right track?


function findLongestWordLength(str) {
 var strLength = "";
 var strLimit = "";
 var array = [];
 var arrayLen = 0;

 //if string is null return ""
 var longest = str[0];
 if (str.length < 1){
   return "";
 }

 //split str into insular elements
 array = str.split(" ");
 console.log(array);

//determine length of those elements
 arrayLen = array.length;
 console.log(arrayLen);

for(var i=0;i<arrayLen.length;i++) {
  if(array.length > i) {
  strLimit = str[i];
 if(strLength.length < strLimit.length) {
   strLength.length = str[i];
  }
   
  }
}


return strLength.length;
}

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

//create function that runs string and returns the string's length
//create var that transforms string into array
//create var that stores largest unit of string array
// separate string array into units
//find unit with largest link
//transform this unit into numerical amount
//return numerical amount


I figured it out using .map(), but I’m still perplexed as to how my loop should work. I know that my condition should be something related to a max limit being the stopping point of the loop. Originally I thought that that was what my strLimit was doing be interrogating all the numerical values known as i and then saying that any length lesser than the strLimit would be looped through with the ultimate result being that the length of strLength would be equal to strLimit. The assignments of strLimit and strLength being equal, I could return strLength and receive the correct answer.

This feels convoluted to me. If anyone can lead me in the correct direction of how my loop should process then I would appreciate it.

Sorry if I miss too much, but this thread is pretty long and I’ll just read the first post.

When you use the split function on your array, you want to make sure your first argument is “” with a space, like this: " ". Don’t add another argument here.

After that, use a loop or create a new array with lengths of each word of the original array. That’s the main thing missing in your first post.

When you have an array with numbers (made of lengths of each string) try to find the highest one. There are a couple ways to do that and Math.max is one example, but you can’t just pass it an array, doesn’t work like that. You can use a spread operator (…) to pass the array into the math.max function and you can use some other ES6 array functions to find the highest value, like sort or reduce. The algorithms came after ES6 section so I’ll assume you know them.

If you want to use <ES6 functions then you can also create a for loop to find the highest value.