Error at "Find the Longest Word in a String"

Hi here! I’m new, and I’ve written a new solution (I think) to this problem.
The thing is that the system give me error when I try to run the test, and don’t know why because the console give me the right number.

function findLongestWordLength(str) {
  let palabras = str.split(" ");
  let palabritasarr = [];

  palabras.forEach(funcion);
  
  function funcion(item){
    let x = item.length;
    palabritasarr.push(x)
  }
  
  var max = Math.max(...palabritasarr);
  return console.log(max)
} 
 

findLongestWordLength("What is the average airspeed velocity of an unladen swallow"); 

You can’t return a console log. console.log returns undefined, so your function returns undefined.

1 Like

Oh thanks!! I didn’t know, and since the console gives the correct number and not error or undefined, I thought it was okay to use it that way.

Thanks JeremyLT!!! Here is the right code:

function findLongestWordLength(str) {
  let palabras = str.split(" ");
  let palabritasarr = [];

  palabras.forEach(funcion);
  
  function funcion(item){
    let x = item.length;
    palabritasarr.push(x)
  }
  
  var max = Math.max(...palabritasarr);
  return (max);
} 
 

findLongestWordLength("What is the average airspeed velocity of an unladen swallow");
1 Like

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