Challenge I'm Currently Tackling:
To solve this challenge, create a function named "findShortestWordAmongMixedElements".
The task is to find and return the shortest string from an array that contains mixed element types.
Important Notes:
* In case of ties, the function should return the first occurrence in the given array.
* The array will have values other than strings.
* If the array is empty, it should return an empty string.
* If there are no strings in the given array, it should return an empty string.
This is my current implementation:
function findShortestWordAmongMixedElements(array) {
if (array.length === 0)) {
return '';
}
var result = array.filter(function (value) {
return typeof value === 'string';
});
var shortest = result.reduce(function (a, b) {
return a.length <= b.length ? a : b;
});
return shortest;
}
var newArr = [ 4, 'two','one', 2, 'three'];
findShortestWordAmongMixedElements(newArr);
//returns 'two'
My code works fine except for passing the test where "no strings are present in the given array". Any hints on how to tackle this scenario or suggestions to optimize the function?