0

I'm a code newbie and I was given a challenge to find the longest word in an array of strings. I followed an example for finding the longest string in a sentence and came up with this:

function longestString(strs) {
  return strs.sort(function(a, b) {return b.length - a.length})[0];
}

longestString('boop', 'bloomburg', 'hello');

it didn't work, and I don't know what's wrong

Eddie
  • 26,593
  • 6
  • 36
  • 58
D. Reichardt
  • 47
  • 1
  • 1
  • 6
  • Just look at what you need as argument over what are you passing? so if you want to pass multiple elements then make function arg as array or simply use rest operator https://javascript.info/rest-parameters-spread-operator#spread-operator – RohitS Jul 04 '18 at 16:55
  • If you're not actually sorting - just want the one longest value - use `reduce()` over `sort()` and avoid multiple loops. – SamVK Jul 04 '18 at 17:14

6 Answers6

5

You're not passing an array, and don't use sort for that, it's the slowest possible way. You can simply use a for loop or to have a bit nicer syntax, use reduce instead.

console.log(longestStringForLoop(['boop', 'bloomburg', 'hello']));
console.log(longestStringReduce(['boop', 'bloomburg', 'hello']));

function longestStringForLoop(arr) {
  let word = "";
  for (let i = 0; i < arr.length; i++) {
    if (word.length < arr[i].length) {
      word = arr[i];
    }
  }
  return word;
}

function longestStringReduce(arr) {
  return arr.reduce((a, b) => a.length < b.length ? b : a, "");
}

Note the big difference between passing multiple strings like you did

longestString('boop', 'bloomburg', 'hello');

and passing an array of strings

longestString(['boop', 'bloomburg', 'hello']);
baao
  • 71,625
  • 17
  • 143
  • 203
4

You can use rest parameter (...) syntax allows us to represent an indefinite number of arguments as an array.

function longestString(...strs) {
  return strs.sort(function(a, b) {return b.length - a.length})[0];
}

console.log(longestString('boop', 'bloomburg', 'hello'));

Doc: rest parameter


One option also is using reduce instead of sort. Using reduce is less reiteration than using sort

function longestString(...strs) {
  return strs.reduce((c, v) => c.length > v.length ? c : v);
}

console.log(longestString('boop', 'bloomburg', 'hello'));
Eddie
  • 26,593
  • 6
  • 36
  • 58
1

When you call longestString:

longestString('boop', 'bloomburg', 'hello');

...that passes three string arguments, not a single array-of-strings argument. You can convert the call to passing an Array:

longestString(['boop', 'bloomburg', 'hello']);

...or for modern JS you can change the function accept variable arguments with ...:

function longestString(...strs) {
  return strs.sort(function(a, b) {return b.length - a.length})[0];
}
Jacob
  • 77,566
  • 24
  • 149
  • 228
1

I appreciate the help! I actually got the answer using a for loop

function longestString(strs) {
let longest = '';
for (let i = 0; i < strs.length; i++) {
if (strs[i].length > longest.length)
longest = strs[i];
}
return longest;
}
D. Reichardt
  • 47
  • 1
  • 1
  • 6
-1

Here is one way to do it:

function longestword (str){
   if(str == undefined || str == null)
      return null;
   var w = str.split(" ");
   var result = null;
   int count = 0;
   for(var i = 0; i < w.lenght; i++){
      if(w[i].length > count){
          count = w[i].lenght;
          result = w[i];
      }
   }
   return result;
}

Call function passing string.

var lword = longestword("What is the longest word of my string?");
-3

Strs is not an array. It's a string.

So when you run .sort method, it only works on arrays.

There are plenty of ways to make it work but in ES6, there are rest parameter which allows us to pass strings as arrays easily and quickly:

function longestString(...strs) {
  return strs.sort(function(a, b) {return b.length - a.length})[0];
}

longestString('boop', 'bloomburg', 'hello');
Boy pro
  • 452
  • 4
  • 19