How to obtain the longest words in a multidimensional array?

Viewed 134

I'm trying to find the way to obtain the longest words of each array:

var carnivores = ['lion', 'shark', 'wolve', 'puma', 'snake'];
var herbivores = ['elephant', 'giraffe', 'gacelle', 'hippo', 'koala'];
var omnivores = ['human', 'monkey', 'dog', 'bear', 'pig'];

Then I create a multidimensional-array: var animals = [carnivores, herbivores, omnivores];

In order to obtain the longest words of each array I do this:

var carnivores = ['lion', 'shark', 'wolve', 'puma', 'snake'];
var herbivores = ['elephant', 'giraffe', 'gacelle', 'hippo', 'koala'];
var omnivores = ['human', 'monkey', 'dog', 'bear', 'pig'];
var animals = [carnivores, herbivores, omnivores];

function longestAnimals() {
  var longest = "";
  for (var i = 0; i < animals.length; i++) {
    for (var j = 0; j < animals[i].length; j++) {
      if (animals[i][j].length > longest.length) {
        longest = animals[i][j];
      }
    }
  }
  return longest;
}

console.log(longestAnimals());

My code returns the longest word of all the array, it shows only elephant. What can I do to obtain shark, elephant, and monkey. Thank you!

5 Answers

Make a function that gets the longest word in one array, then map it onto the other functions:

function longest(arr) {
    let longest = "";
    for (const ele of arr) {
        if (ele.length > longest.length) {
            longest = ele;
        }
    }
    return longest;
}

function longestInEach(arr) {
    return arr.map(v => longest(v));
}

const carnivores = ['lion', 'shark', 'wolve', 'puma', 'snake'];
const herbivores = ['elephant', 'giraffe', 'gacelle', 'hippo', 'koala'];
const omnivores = ['human', 'monkey', 'dog', 'bear', 'pig'];
const animals = [carnivores, herbivores, omnivores];

console.log(longestInEach(animals)) // ["shark", "elephant", "monkey"]

Now you are looping and finding only one value.

It is needed to make the longest as array and whenever the j loop finishes, push the item for that sub array as follows.

var carnivores = ['lion', 'shark', 'wolve', 'puma', 'snake'];
var herbivores = ['elephant', 'giraffe', 'gacelle', 'hippo', 'koala'];
var omnivores = ['human', 'monkey', 'dog', 'bear', 'pig'];

var animals = [carnivores, herbivores, omnivores];
function longestAnimals() {
  var longest = [];
  for (var i = 0; i < animals.length; i++) {
    var longestItem = "";
    for (var j = 0; j < animals[i].length; j++) {
      if (animals[i][j].length > longestItem.length) {
        longestItem = animals[i][j];
      }
    }
    longest.push(longestItem);
  }
  return longest;
}
console.log(longestAnimals());

One line of functional solution using Array.prototype.sort() and Array.prototype.map() method. Just sort each inner array by string length in descending order and return the first element of each array.

const carnivores = ['lion', 'shark', 'wolve', 'puma', 'snake'];
const herbivores = ['elephant', 'giraffe', 'gacelle', 'hippo', 'koala'];
const omnivores = ['human', 'monkey', 'dog', 'bear', 'pig'];
const animals = [carnivores, herbivores, omnivores];

const longestAnimals = (arr) =>
  arr.map((x) => x.sort((a, b) => b.length - a.length)[0]);

console.log(longestAnimals(animals));

You can also use Array.prototype.reduce to return the longest string in each array:

var carnivores = ['lion', 'shark', 'wolve', 'puma', 'snake'];
var herbivores = ['elephant', 'giraffe', 'gacelle', 'hippo', 'koala'];
var omnivores = ['human', 'monkey', 'dog', 'bear', 'pig'];
var animals = [carnivores, herbivores, omnivores];

var longestString = function(arr) {
  return arr.reduce(
    function (a, b) { return a.length > b.length ? a : b; }
  );
}

var longest = animals.map(function(arr) {
  return longestString(arr);
})

console.log(longest);

Reduce the input arrays using per array sorting to find the longest word:

const carnivores = ['lion', 'shark', 'wolve', 'puma', 'snake'];
const herbivores = ['elephant', 'giraffe', 'gacelle', 'hippo', 'koala'];
const omnivores = ['human', 'monkey', 'dog', 'bear', 'pig'];

console.log(longestWords(carnivores, herbivores, omnivores));

function longestWords(...arrays) {
  return arrays.reduce( (acc, arr) => 
    [...acc, arr.sort( (a, b) => b.length - a.length).shift()], []);
}

Related