Advertisement
dimipan80

Exams - Ewoks Fans

Dec 22nd, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You have a list of birthdays. People born on 25.05.1973 and after are fans of Ewoks and these before that
  2.  – haters. The biggest hater is the oldest person born before 25.05.1973. The biggest fan is the youngest person
  3.  born after 25.05.1973. Find the biggest fan and hater of Ewoks. Exclude people born on or before 01.01.1900 and
  4.  those born on and after 01.01.2015, because most of them are either dead or not born yet.
  5.  The input data comes as array of strings containing dates in the format “dd.mm.yyyy”.
  6.  Print at the console when the biggest fan and the biggest hater of Ewoks were born.
  7.  Use the method "date.toDateString()" to format the date at the output.
  8.  If there is only one fan or hater, print out the date of his birth. If there are no people between 01.01.1900
  9.  and 01.01.2015 print “No result”. */
  10.  
  11. "use strict";
  12.  
  13. function printFansAndHatersOfTheEwoks(args) {
  14.     var bornDatesArr = [];
  15.     for (var i = 0; i < args.length; i += 1) {
  16.         var dateStr = args[i].split('.').filter(Boolean);
  17.         var bornOnDate = new Date(dateStr[2] + '-' + dateStr[1] + '-' + dateStr[0]);
  18.         if (bornOnDate > new Date('1900-01-01') && bornOnDate < new Date('2015-01-01')) {
  19.             bornDatesArr.push(bornOnDate);
  20.         }
  21.     }
  22.    
  23.     if (bornDatesArr.length > 1) {
  24.         bornDatesArr.sort(function (date1, date2) {
  25.             return (date1 > date2) ? 1 : (date1 < date2) ? -1 : 0;
  26.         });
  27.     }
  28.    
  29.     var borderDate = new Date('1973-05-25');
  30.     if (!bornDatesArr.length) {
  31.         console.log('No result');
  32.     } else {
  33.         if (bornDatesArr[bornDatesArr.length - 1] >= borderDate) {
  34.             console.log('The biggest fan of ewoks was born on ' +
  35.             bornDatesArr[bornDatesArr.length - 1].toDateString());
  36.         }
  37.         if (bornDatesArr[0] < borderDate) {
  38.             console.log('The biggest hater of ewoks was born on ' +
  39.             bornDatesArr[0].toDateString());
  40.         }
  41.     }
  42. }
  43.  
  44. printFansAndHatersOfTheEwoks(['22.03.2014', '17.05.1933', '10.10.1954']);
  45. printFansAndHatersOfTheEwoks(['22.03.2000']);
  46. printFansAndHatersOfTheEwoks(['22.03.1700', '01.07.2020']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement