All Longest Strings

All Longest Strings

Given an array of strings we look for the string with the most characters inside. They can be one , two etc…

Example
For inputArray = [‘aaa’,’ss’,’bbb’,’a’] => [‘aaa’,’bbb’]

let inputArray = ['aaa','ss','bbb','a']

function allLongestStrings(inputArray){
  let longestLength = 0;
  const longestWords = [];
  
  
  inputArray.forEach(word => {
    longestLength = longestLength < word.length ? word.length : longestLength;
    
  })
  
  inputArray.forEach(word => {
    if(word.length === longestLength){
      longestWords.push(word)
    }
  })
  
  return longestWords;
}

console.log(allLongestStrings(inputArray))  ['aaa','bbb']

First we create a function called allLongestStrings with parameter inputArray and two variables longestLenght = 0 and longestWords = []

We are going to iterate through each one with forEach loop from inputArray and callback function with parameter word and we check longestLength = longestLength < word.length ? word.length : **longestLength
This reads check if longestLength is less than word.length and if it is less return word.length else return longestLength.

We are going to iterate again through each one with forEach loop from inputArray and callback function with parameter word and we check if word.lenght === longestLength we push to the longestWords.push(word)

In the end we return longestWords

comments powered by Disqus