Finding the longest string in an array in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in an array of strings. Our function should iterate through the array and find and return the longest string from the array.

Our function should do this without changing the content of the input array.

Example

The code for this will be −

const arr = ["aaaa", "aa", "aa", "aaaaa", "acc", "aaaaaaaa"];
const findLargest = (arr = []) => {
   if(!arr?.length){
      return '';
   };
   let res = '';
   res = arr.reduce((acc, val) => {
      return acc.length >= val.length ? acc : val;
   });
   return res;
};
console.log(findLargest(arr));

Output

And the output in the console will be −

aaaaaaaa
raja
Published on 24-Nov-2020 10:19:49
Advertisements