When I run this:
var array= [123, ["hello", 67, 888888], "try", {key: "key", value: "this is the longest String"}]
longestString(array);
correct answer:
>>>"this is the longest String"
current answer:
>>>"try"
How can I update this function to pass the above test and return the correct answer?
below is the longestString() function:
function longestString(array) {
// return the longest string in the array
var longest = 0;
var longestString = "";
for (var i = 0; i < array.length; i++) {
if (array[i].length > longest && typeof array[i] =="string") {
longest = array[i].length;
longestString = array[i];
}
}
return longestString;
}
'tiny'(4)"tiny"? Do you want to compare every Array element, no matter how nested it is? What about objects with key-value pairs or ES6 Symbols? How should these cases be handled?