0

Given an array of mixed types, "getLongestWordOfMixedElements" returns the longest string in the given array.

Notes:

  • If the array is empty, it should return an empty string ("").
  • If the array contains no strings; it should return an empty string.

How do I find out if the array contains a string or not, as in this code:

function getLongestWordOfMixedElements(arr) {

    if (arr.length === 0) return ""
    var max = 0
    for (var i = 0; i < arr.length; i++){
        if(arr[i].length > max) max = arr[i]
    }
    return max
}

getLongestWordOfMixedElements([3, 'word', 5, 'up', 3, 1]);
mkj
  • 2,761
  • 5
  • 24
  • 28

3 Answers3

4

You can filter the array for strings then use a reduce operation to find the longest one

function getLongestWordOfMixedElements(arr) {
  return arr
    .filter(item => typeof item === 'string')
    .reduce((longest, str) => str.length > longest.length ? str : longest, '');
}

console.log(getLongestWordOfMixedElements([3, 'word', 5, 'up', 3, 1]));

Note that if any words are the same length, the earlier one in the array is returned.


Additionally, you could skip the filter and do the type check in the reduce...

return arr.reduce((longest, str) => {
  return typeof str === 'string' && str.length > longest.length ?
    str : longest;
}, '')
Phil
  • 157,677
  • 23
  • 242
  • 245
0

You iterate over the element and check if its type is a string. You can do so with typeof.

Let us say you would've a bunch of data and would not like to double your memory usage / or for the sake of the example in your code:

function getLongestWordOfMixedElements(arr) {
    var max = "";

    if (arr.length) {
        arr.forEach(function (item) {
            if (typeof item === "string" && item.length > max) {
                max = item;
            }
        });
    }

    return max;
}


console.log(getLongestWordOfMixedElements([3, 'word', 5, 'up', 3, 1, {foo:4}]));

In your code you would change it this way:

for (var i = 0; i < arr.length; i++) {
    var item = arr[i];

    if (typeof item === "string" && item.length > max) {
        max = arr[i];
    }
}
Stefan Rein
  • 8,084
  • 3
  • 37
  • 37
0

Well here's my version of it...

function getLongestWordOfMixedElements(arr) {
  let result = '';

  if (arr.length) {
    for(i in arr) {
      const value = arr[i];

      if (value && typeof value === 'string' && value.length > result.length) {
        result = value;
      }
    }
  }

  return result;
}


getLongestWordOfMixedElements([333333, 'word', 5, 'up', 3, 1]);
Bruce Smith
  • 811
  • 8
  • 14
  • 1
    [Why is using “for…in” with array iteration a bad idea?](http://stackoverflow.com/q/500504/1529630) – Oriol Oct 27 '16 at 14:49
  • I don't really see those as valid reasons. It's like if you make sloppy code, the for in loop is going to break. – Bruce Smith Oct 27 '16 at 16:35
  • Iterating enumerable inherited properties is not the main problem. The main problem is that iteration order is not well-defined, so in case there are multiple string values with the same length you can get inconsistent results – Oriol Oct 28 '16 at 01:34