I am trying to determine the length of the longest string in a given array. If the array is empty, the function should return 0.
Here is my attempted solution:
function getLengthOfLongestElement(arr) {
var longestLength = 0;
for(var i=0; i< arr.length; i++){
if(arr[i].length > longestLength){
longestLength = arr[i].length;
}
}
}
var output = getLengthOfLongestElement(['one', 'two', 'three']);
console.log(output); // --> SHOULD RETURN 5
However, my code did not work as expected. Do you have any insights on how to fix it or any better alternatives to achieve this task?