Converting a string to an array

Tell us what’s happening:
Okay so I have been trying to solve this one for the past 2 hours using different approaches(see the commented out part) but have failed to reach the solution even though I’ve figured the logic. All approaches failed because they need an array to operate upon. I thought first declaring an array as var arr = [ ] and then assigning str1[i].length; to arr would do it but I was wrong. Can someone help me with this?

Your code so far

var arr = [];
var arr1 = [];
function findLongestWord(str) {
  str1 = str.split(' ');
  for (var i=0; i<str1.length; i++) {
    arr += str1[i].length;    
}
  arr1 = JSON.parse("[" + arr + "]");
//   return typeof(arr1);
  

//   return Math.max(...arr); 
//   arr1.sort(function(a,b) {
//   return b - a;
// });
  
//   return Math.max.apply(null, arr);

}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36.

Link to the challenge:

Yes. It worked. Thank you so much. Some questions:

  1. Why does declaring arr globally cause problems? There are no problems if I declare str1 globally. Just FCC issues or is it a bad way of writing code?
  2. The spread syntax and apply method I found online after some researching. Would you recommend using the two approaches? Any issues? Also, I didn’t understand the apply method properly.
  3. When I use the spread syntax, I also get a warning saying “spread/rest operator is only available in ES6(use esversion:6”. Though it does work. What is that?

Your answer totally cleared up my confusion regarding the use of global and local variables. Also I’ll keep your other points in mind as well. Thanks for the insightful answers.