Longest string in the array

Medium
1
36.4% Acceptance

In this coding challenge, you will be implementing a function called findLongest(array) that takes an array of strings array as the argument. The function should return the longest string in the array.

Instructions

  • If the input array is empty, then return null.
  • If two or more strings have the same length, return the one with a lower index.

Example test cases

findLongest(['cat', 'dog', 'elephant']); // Output: 'elephant' findLongest(['apple', 'banana', 'pear']); // Output: 'banana' findLongest(['', 'a', 'aa', 'aaa']); // Output: 'aaa' findLongest([]); // Output: null

Hints

  • You need to iterate over the array and check the length of each string individually. Make sure to keep count of the current longest string.
  • You can use the built-in method .reduce().