Advertisement
dimipan80

Find Youngest Person

Nov 15th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function findYoungestPerson(persons) that accepts as parameter an array of persons,
  2. finds the youngest person and returns his full name. Write a JS program youngestPerson.js to execute your function
  3. for the below examples and print the result at the console. */
  4.  
  5. "use strict";
  6.  
  7. function findYoungestPerson(persons) {
  8.     persons.sort(function(a, b) {
  9.         return a.age - b.age;
  10.     });
  11.  
  12.     return 'The youngest person is ' + persons[0].firstname +
  13.         ' ' + persons[0].lastname;
  14. }
  15.  
  16. var persons = [
  17.     { firstname: 'George', lastname: 'Kolev', age: 32},
  18.     { firstname: 'Bay', lastname: 'Ivan', age: 81},
  19.     { firstname: 'Baba', lastname: 'Ginka', age: 40}];
  20.  
  21. console.log(findYoungestPerson(persons));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement