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:

Instead of:

arr += str1[i].length;

you should be using:

arr.push(str1[i].length); // this adds each word length to arr    

I recommend declaring arr = [] inside the function instead of globally to avoid problems with the FCC test cases.

When the for loop finishes with the latest code change I gave you, then only one of the commented lines of code will give you the correct solution.

FYI - You do not need arr1 at all.

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?

Answers to your questions:

#1) In general it is best not to create global variables, because sometimes it can cause unintended problems. In the case of the FCC test cases, think of them as being called one after another right after the code you write.

So it would look kind of like below:

var arr=[];
function findLongestWord(str) {
  Your function goes is in here
findLongestWord("Test one string");
findLongestWord("Test two string");
findLongestWord("Test one string");

Since you are pushing values to arr when ever findLongestWord is called and arr is defined globally, arr will not get reset back to [] each time findLongestWord is called (during the tests). The first test will pass, but each subsequent test will start with arr having the elements it had during the last call to findLongestWord.

The reason str1 was not a problem in your solution is because each time when findLongestWord was called, it was assigned the result of splitting str into an array.

#2) You should not be using methods and functions you do not understand. My suggestion is to either do the research on how the spread operator would work in your solution and why using Math.max.apply(null, arr) worked for your solution. Also, I would recommend reworking the challenge using what you already knew going into the challenge. This particular challenge can be solved without using any arrays at all.

#3) I believe the reason you can use ES6 syntax in your solutions is because the tests behind the scenes are written using ES6 syntax. If you are using the most recent version of most browsers, they already understand ES6 syntax.

1 Like

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.