Guest User

Untitled

a guest
May 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. //Given an array, "findShortestWordAmongMixedElements" returns the shortest string within the given array.
  2. //
  3. // Notes:
  4. // * If there are ties, it should return the first element to appear in the given array.
  5. // * Expect the given array to have values other than strings.
  6. // * If the given array is empty, it should return an empty string.
  7. // * If the given array contains no strings, it should return an empty string.
  8. function findShortestWordAmongMixedElements(arr) {
  9. ret = '';
  10. arr.forEach(x => {
  11. if (typeof x == 'string' || x instanceof String) {
  12. if (!ret || x.length < ret.length) {
  13. ret = x;
  14. }
  15. }
  16. });
  17. return ret;
  18. }
  19. var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
  20. console.log(output); // --> 'two'
Add Comment
Please, Sign In to add comment