Being new to coding, I recently encountered a challenge where I had to identify the longest word in an array of strings. After referring to a method for finding the longest string in a sentence, I attempted the following code:
function findLongestWord(array) {
return array.sort(function(a, b) {return b.length - a.length})[0];
}
findLongestWord('apple', 'banana', 'carrot');
Unfortunately, the code did not produce the desired result and I am unsure of what went wrong.