Find the Longest String- Not accepting my answer

Find the Longest String- Not accepting my answer
0

#1

I need help understanding why my code is not being accepted for this challenge. I understand that the way it is set up is a bit rough but it will output the correct answer to every input that the challenge can throw at it. Maybe I’ve misunderstood something? Thank you in advance for the help.

-Noah


#2

I’m very confused by what you are trying to do with commas. I suggest using a tool like repl.it with a easy-to-use console, so you can check what each step of your code is doing.

In the future, please share code instead of screenshots.
When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

Okay thx I will do that next time. Also, the commas are just to prep the data coming into longestArr so it can be split and sorted in ascending order. Using spaces would have done the same thing but it was more for my sake when I was trying to read the array coming in.

Also, here is my code for reference.

function findLongestWord(str) { 
  var longestWord = "";
  var longestArr = [];
  
  var wordArr = str.split(" ");
  
  for (var i = 0; i < wordArr.length; i++) {

      longestArr += wordArr[i].length + " ";
}   
  longestArr = longestArr.split(" ").sort(function(a,b) {
    return a - b;
  });
  longestWord = longestArr.pop();
  return longestWord;
}

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

#4

I figured it out the straight forward way to do it by reading the cheat page on the challenge. I tried to do it like they wanted me to originally but it kept telling me that wordArr[i].length was undefined so I got frustrated and tried the route that I posted about here.

function findLongestWord(str) { 
   var maxLength= 0;
  
  var wordArr = str.split(" ");
  
  for (var i = 0; i < wordArr.length; i++) {
    if (wordArr[i].length > maxLength) {
      maxLength = wordArr[i].length;
      
    }
  }
  return maxLength;
}

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

#5

longestArr is already an array. You don’t need to split it.


#6

longestArr starts off as an array, but then in your for loop, you change it to a string of numbers (lengths) separated by spaces. Then you convert it back to an array and attempt to sort it using a sort function meant for numbers and not strings. Finally, you pop off the last item in longestArr and assign it to longestWord. When you return longestWord it is a string and the challenge is expecting a number.

Why not keep longestArr as an array the entire time? Instead of using += which converts longestArr to a string, just use the push function on longestArr and add the lengths (which will actually be numbers). Then, the rest of your code will work correctly (including the sort function).


#7

Ahhhhhh I didn’t realize that it was outputting a string! Yep I did what you suggested and it works perfectly. Thanks!