Michael Stokes
Michael Stokes

Reputation: 401

Objects: How to return the name of a property with the highest value?

I have a function that accepts an array of objects called 'mountains'. They contain the name and elevation properties. The goal is to return the name of the mountain that has the highest elevation. Here is my code so far: Note: This code throws a Type Error unfortunately.

    function tallest(mountains) { 

   mountains = [
    {name: 'Mount Everest', elevation: 29029},
    {name: 'Mount Kilimanjaro', elevation: 19341 },
    {name: 'Mount Diablo', elevation: 3848 }
    ];

   var highestElevation = mountains.map(function(mountain) {
     return {
       name: mountain.name,
       highestMountain: Math.max.apply(Math, mountain.elevation)};
   });

      var tallestMountain = mountains.sort(function(a,b) {
        return b.highestElevation - a.highestElevation;
      })[0];
}

console.log(tallest()); 

What is a valid solution to return the name of the mountain with the highest elevation?

Upvotes: 0

Views: 103

Answers (4)

user120242
user120242

Reputation: 15268

Reduce iterates over all the mountains. Checks if current elevation is higher than the maximum seen.
You don't really need to sort, since you're only after the maximum.

function tallest(mountains) {

  mountains = [{
      name: 'Mount Everest',
      elevation: 29029
    },
    {
      name: 'Mount Kilimanjaro',
      elevation: 19341
    },
    {
      name: 'Mount Diablo',
      elevation: 3848
    }
  ];

  var tallest = mountains.reduce(function(max,mountain) {
    return mountain.elevation > max.elevation ? mountain : max
  });
  
  return tallest.name;
}

console.log(tallest());

Also note that you asked to return the name of the mountain.

Upvotes: 1

User863
User863

Reputation: 20039

Using sort()

function tallest(mountains) {
  var mountains = [
    {name: 'Mount Everest', elevation: 29029},
    {name: 'Mount Kilimanjaro', elevation: 19341 },
    {name: 'Mount Diablo', elevation: 3848 }
  ];

  mountains.sort((a, b) => a.elevation - b.elevation);  
  
  return mountains.pop();
}

console.log(tallest());

Upvotes: 0

Guvaliour
Guvaliour

Reputation: 417

let   mountains = [{
      name: 'Mount Everest',
      elevation: 29029
    },
    {
      name: 'Mount Kilimanjaro',
      elevation: 19341
    },
    {
      name: 'Mount Diablo',
      elevation: 3848
    }
  ];

let name = mountains.reduce((acc,ele)=>{
 return acc.elevation<ele.elevation ? ele.elevation : acc;
},mountains[0])['name'];
console.log(name)

Upvotes: 1

Udbhav
Udbhav

Reputation: 212

This is the most optimized one, with one single iteration:

const mountains = [
    {name: 'Mount Everest', elevation: 29029},
    {name: 'Mount Kilimanjaro', elevation: 19341 },
    {name: 'Mount Diablo', elevation: 3848 }
];

const getTallest = () => {
    let tallest = mountains[0];

    mountains.forEach((mountain) => {
        if (mountain.elevation > tallest.elevation) {
            tallest = mountain;
        }
    });
    return tallest;
};

getTallest(mountains);

Upvotes: 0

Related Questions