Basic Javascript Algorithm problem ! need Help

Tell us what’s happening:
I tried to solve the one of the basic algorithm problem about finding lonest word and returns a numer of that. It seems working but the last argument is not working well. I don’t understand why???

Your code so far

function findLongestWordLength(str) {
  let arr = [];
  str = str.split(" ");
  for (let i=0; i < str.length; i++) {
    arr += str[i].length;
  }
  
  return Math.max(...arr);
}

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

function findLongestWordLength(str) {
let arr = [];
str = str.split(" ");
for (let i=0; i < str.length; i++) {
  arr += str[i].length;
}

return Math.max(...arr);
}

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_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:

console.log() is your friend.
Try using console.log(arr) to see if you can discover the problem.

Hi well, I tried that too. But. It. seems it’s returning different values. but. it. works. on other arguments.

It works on single digit answers because of what arr is. What does arr look like?

What does your console.log(arr) log?

And then, what are you telling your function to do with return Math.max(...arr);?

1 Like

if you are not familiar withconsole.log (you should become, tho, it can be your best friend) you can use this tool to see what happens to the values in your code: http://pythontutor.com/javascript.html