Find the Longest Word in a String with Math.max?

Tell us what’s happening:
would anyone know how to make this code work?
i feel like the map function combined with the lenght and Math.max should be able to resolve this problem.

Your code so far


function findLongestWordLength(str) {
 let result =  [str.split(' ')];
 let x = result.map(function (){
   return Math.max (result.length);
 });
  return (x);
}
console.log(result);
console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"));
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.11; rv:69.0) Gecko/20100101 Firefox/69.0.

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

The problem is that str.split will automatically create an Array for you, you do not need to add it inside of an Array. In fact, you created this:

result = [ ['string'] ]

The second problem I see is with debugging, the console.log(result) will produce and undefined, because it is outside of it’s scope, let and const variables create a block scopes and so “result” is not accessible for that console.log (it would work if you added it inside of the “findLongestWordLength” body.)

For map to work like you defined it, you need to pass it a parameter, which will the iterable so to speak, like the “i” in a for loop.

 let x = result.map(function ( el ){
   return el;
 });

In order for Math.max to work, you need to pass it a range of numbers, as stated in MDN:

" The Math.max() function returns the largest of zero or more numbers.", you are passing it just 1 value.

I think you need to reasses hw you approached the problem.

Good job splitting the first Array (just fix it as stated above), then I personally would initiate a variable, to store the value of the current longest word…That could be result[0] when starting out for example.

I would then loop (loop instead of mapping, mapping will just return a new Array) over the Array and count the characters of the current word.

Is the current word longer than the word you have stored in result[0]? then store this new word. It is not? Continue the loop.

At the end you will have the longest word.

1 Like

Thanks, I just changed that, but now I get 9. I thikn now the problem is the .map function.

function findLongestWordLength(str) {
 let result =  str.split(' ');
 cosnole.log(typeof result);
 let x = result.map(function (){
   return Math.max (result.length);
 });
  return (x);
}

I have edited my answer covering the whole code.

Hey guys thanks for the pointers, it really helped a lot.
I think I found a solution, I posted it below. I think its possible to use Math.max. Although, I will try to answer it the other way just to have different solutions.

function findLongestWordLength(str) {
 let result =  str.split(' ');
 let x = result.map(function (y){
   return y.length;
 });
  let s = Math.max(...x);
  return s; 
}
console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"));

That’s great! You can also just return this:

return Math.max(...x);

Without initiating the variable “s”

I honestly didnt think of it, I feel its a bit easier to understand than some of the solutions of FCC. I wonder if it maybe creates some bugs or because of ES6 they decided to go with the loop.

Hi guys, I used this and it works but when I run the tests, it doesn’t

function findLongestWord(str) {
  let arr;
 arr = str.split(' ');
  str = '';
  for (let i = 0; i < arr.length; i++) {
    if(str.length > arr[i].length) {
    }else {
      str = arr[i];
    }
  }
  console.log(str.length);
  return str.length;
}

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