Find the Longest Word in the String


#1

I’m purposefully not viewing the hints provided by fCC (The help is too good. I scroll down to the spoiler section). I’d like to know where I’m going wrong with this code. Just tips, please. Thank you in advance.

function findLongestWord(str) {
  str = str.split(' ');
  var allLengths = [];
  for (var i = 0; i < str.length; i++) {
    length = str[i].length;
    allLengths.push(length);
  }
  var lowToHigh = allLengths.sort();
  return lowToHigh[-1];
}

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

#2

you should check what’s in lowToHigh with

console.log(JSON.stringify(lowToHigh))

then read this post on the array sort function


#3

Is there a specific javascript editor that you’d recommend for this? Not sure how to make this check.


#4

the output from console.log shows up in your browser console window

here’s info for chrome


#5

You can also code your solutions in an online editor/compiler like replit.com and then paste your solution back into FCC to run the tests. You would be able to view console without going into Chrome’s developer tools.


#6

The message I’m receiving from the editor is that var lowToHigh = allLengths.sort(); is undefined. What is making it ‘undefined’?

ReferenceError: lowToHigh is not defined
at eval:11:28
at eval
at n.


#8

Although my approach to solving the problem is quite different from the other approach, I don’t understand why it still can’t work.


#9

That’s probably from console.log(JSON.stringify(lowToHigh)) being outside the function - lowToHigh is a local variable not visible outside the function - fix is to move the console.log inside the function


#10

You actually have two problems with the solution.
First, your current sort function will not work as you intend it to work for some of the tests, so definitely read more about the sort function
Second, lowToHigh[-1] is going to be undefined, which is why you are seeing undefined. You need to think about how to get the last item in the array.


#11

Negative indexing as reverse indexing does not work for arrays in javascript - if you want the last index it’s one less than the array length


#12

This is helpful. I’m looking at this page to learn more about the sort method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort.

I read this:
var scores = [1, 10, 21, 2];
scores.sort(); // [1, 10, 2, 21]

Then, I read a description of the difference between
arr.sort() and arr.sort(compareFunction).

Finally, I read: If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in Unicode code point order. In a numeric sort, 9 comes before 80, but because numbers are converted to strings, “80” comes before “9” in Unicode order.

Are the numerical values in scores.sort(); // [1, 10, 2, 21] actually strings or have they been converted back to ints?


#13

Good question - array sort has a few pitfalls - the first is it sorts the array in-place - it does not generate a new array - this means the array elements are the original elements not copies - they are just moved around in the array - but sorting requires two values to be compared at a time - since javascript arrays are heterogeneous i.e. can have different types of elements the sensible default comparison is to compare values as strings - if a value is not a string to begin with it is converted to a string just for the comparison - the original element remains unchanged

look at this array

scores=[15, 19, "4", 100, "0200"]

after sorting scores the array values are

scores.sort()
["0200", 100, 15, 19, "4"]

the numbers in scores are still numbers, the strings are still strings but you can see the sorting is based on their comparison as strings


#14

It turns out the following code works because the words in the list are shorter than 10 letters long. If there were words two-digits long included, I would need to use sort(compareFunction). In order to clear this particular challenge, I have to use the more inclusive sort method.

function findLongestWord(str) {
  str = str.split(' ');
  var allLengths = [];
  for (var i = 0; i < str.length; i++) {
    length = str[i].length;
    allLengths.push(length);
  }
  var lowToHigh = allLengths.sort();
  return lowToHigh[allLengths.length-1];
}

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

#15

When you sort your array, use this function,

sort(function(a,b) {return a.length - b.length;});