Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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.
- function findShortestWordAmongMixedElements(arr) {
- ret = '';
- arr.forEach(x => {
- if (typeof x == 'string' || x instanceof String) {
- if (!ret || x.length < ret.length) {
- ret = x;
- }
- }
- });
- return ret;
- }
- var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
- console.log(output); // --> 'two'
Add Comment
Please, Sign In to add comment