I wrote a solution for a codeCamp exercise:Write a function called findShortestWordAmongMixedElements.Given an array, findShortestWordAmongMixedElements returns the  shortest string within the given array.Notes:If there are ties, it should return the first element to appear in the given array.Expect the given array to have values other than strings.If the given array is empty, it should return an empty string.If the given array contains no strings, it should return an empty string.Right now I essentially break this function into two parts. The filtering methodto just get the stringsand then reduce to find the shortest one among them. function findShortestWordAmongMixedElements(arr) {  var containsStrings = function(arr){   return arr.every(function(cv){      return Object.prototype.toString.call(cv) !== '[object String]';    });  }, shortestWord;  if ((!(arr.length)) || ((containsStrings(arr)))) return '';   arr = arr.filter(function(e, i, a){      if (typeof e == 'string') {       return e;     }   });   shortestWord = arr.reduce(function(prev, next) {     if (prev.length < next.length) {         return prev;     } else if (prev.length === next.length){        return prev;     } else {        return next;     }   });   return shortestWord;}My take is this is certainly readable which I like and I LOVE the helper function I made for the initial check which uses the every method. Since (at first) I spent a lot of time trying to do all the operations within the body of the callback for the filterI feel this take is like I used flat head screw driver for every screw instead of a phillips...
Function to find the shortest word in an array, where not every element is a string
javascript;programming challenge;array
Probably the shortest way to do this is to use array.filter to only get the strings, array.sort to sort them by length, and get the first item of the resulting array.function findShortestWordAmongMixedElements(arr){  return arr.filter(e => typeof e === 'string').sort((a, b) => a.length - b.length)[0];}Now a few things with your code...First, a mixed-type array is strange. I'm curious as to what the situation is why it's mixed. Anyways, it's best to keep arrays contain only one type to keep it predictable. Having it mixed makes receiving functions do unnecessary type checks, like containsStrings.Also, just don't do type checks. If the function expects an array of strings, then assume it's an array of strings. If it contains something other than a string, let it blow up, let it throw an error. It's the consumer's responsibility to provide the correct input. Again, you'll end up doing unnecessary type checks that will only slow down the code.containsStrings is also unnecessary. It's just a short-circuit if the array doesn't contain strings at all. filter can do that with one less call. If an array does not contain a string, filter will return an empty array, thus reduce won't run. If you give reduce an undefined as initial value, it will return undefined on an empty array.