text: /*Bonfire: Friendly Date Ranges
  Difficulty: 4/5
  
  Implement a way of converting two dates into a more friendly date range that could be presented to a user.
  
  It must not show any redundant information in the date range.
  
  For example, if the year and month are the same then only the day range should be displayed.
  
  Secondly, if the starting year is the current year, and the ending year can be inferred by the reader, the year should be omitted.
  
  Input date is formatted as YYYY-MM-DD
  
  Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
  
  Here are some helpful links:
  
  String.split()
  String.substr()
  parseInt()
  
  Code by Rafael Rodriguez & @guyjoseph
  rafase282@gmail.com
  http://www.freecodecamp.com/rafase282
  */
  
  function friendly(str) {
  
    var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  
    // COnverst a YYYY-MM-DD string into a date object.
    function convertDate(str) {
      // Split the dates to work independently.
      var dateStr = str.split('-');
  
      // Force the dates into Universal time to avoid issues due to timezones.
      return (new Date(Date.UTC(dateStr[0], dateStr[1] - 1, dateStr[2])));
  
    }
  
    // Handles the case of the day's endings.
    function dateEnding(val) {
      switch (val) {
        case 1:
        case 21:
        case 31:
          return val + 'st';
        case 2:
        case 22:
          return val + 'nd';
        case 3:
        case 23:
          return val + 'rd';
        default:
          return val + 'th';
      }
    }
  
    // Checks for the real difference in months to avoid errors
    function monthDiff(date1, date2) {
      var month2 = date2.getUTCFullYear() * 12 + date2.getUTCMonth();
      var month1 = date1.getUTCFullYear() * 12 + date1.getUTCMonth();
      return month2 - month1;
    }
  
    // Get's the right month string.
    function getMonth(date) {
      return months[date.getUTCMonth()];
    }
  
    function displayDate() {
  
      // Handles same day
      if (date2.getTime() - date1.getTime() === 0) {
        return [getMonth(date1) + ' ' + dateEnding(date1.getUTCDate()) + ', ' + date1.getUTCFullYear()];
      }
  
      // Handles same month
      if (date1.getUTCMonth() === date2.getUTCMonth() && date1.getUTCFullYear() === date2.getUTCFullYear()) {
        return [getMonth(date1) + ' ' + dateEnding(date1.getUTCDate()), dateEnding(date2.getUTCDate())];
      }
  
      // Handles more than a month of difference, but less than 12 months
      if (monthDiff(date1, date2) < 12) {
        return [getMonth(date1) + ' ' + dateEnding(date1.getUTCDate()), getMonth(date2) + ' ' + dateEnding(date2.getUTCDate())];
      }
  
      // Handles cases with more than 12 months apaprt.
      return [getMonth(date1) + ' ' + dateEnding(date1.getUTCDate()) + ', ' + date1.getUTCFullYear(), getMonth(date2) + ' ' + dateEnding(date2.getUTCDate()) + ', ' + date2.getUTCFullYear()];
    }
  
    var date1 = convertDate(str[0]);
    var date2 = convertDate(str[1]);
  
    return displayDate();
  
  }
  
  console.log(friendly(['2017-01-01', '2017-01-01'])); //['January 1st, 2017'] --> Same day
  console.log(friendly(['2015-07-01', '2015-07-04'])); //['July 1st','4th'] --> Same month
  console.log(friendly(['2016-03-01', '2016-05-05'])); //['March 1st','May 5th'] --> More than 1 month, less than 1 year
  console.log(friendly(['2015-12-01', '2016-02-03'])); //['December 1st','February 3rd'], --> More than 1 month less than 1 year
  console.log(friendly(['2022-09-05', '2023-09-04'])); //['September 5th, 2022','September 4th, 2023'] --> More than 1 year apart
  console.log(friendly(['2015-12-01', '2017-02-03'])); //['December 1st, 2015','February 3rd, 2017']); --> More than 1 year apart
  