text: Q:
  
  DateTime datesAgo collection function
  
  Hi I need help with tweaking my datesAgo function. What it does is it gives me a collection of dates from the date that is passed through counting backwards but my problem with my function is that it's not showing the 3rd date correctly it should be 17 instead of 16 can someone see if they know why it's happing like that
  
  Date.prototype.datesAgo = function(num) {
      let date = this;
      let arr = [];
  
      for(let i = 0; i < num; i++) {
          arr.push(i.toString());
      }
  
      let days = arr.slice(0, num).join(' ');
  
      console.log(days)
      return days.split(' ').map(function(n) {
  
          date.setDate(date.getDate() - n);
  
          return (function(year, month, day) {
              return [year, month < 10 ? '0'+ month : month, day < 10 ? '0' + day : day].join('-');
          })(date.getFullYear(), date.getMonth(), date.getDate());
      }).join(',');
  }
  
  
  console.log(new Date('2018-05-19').datesAgo(3))
  
  A:
  
  On each iteration, you're mutating the original date object:
  date.setDate(date.getDate() - n);
  
  So, on each subsequent iteration, you're subtracting n from the last iteration's date, not the original date. Clone the original date object on each iteration instead:
  
  Date.prototype.datesAgo = function(num) {
    const date = this;
    const dateStrs = Array.from({ length: num }, (_, i) => {
      const clonedDate = new Date(date.getTime());
      clonedDate.setDate(date.getDate() - i);
      return (function(year, month, day) {
        return [year, month < 10 ? '0' + month : month, day < 10 ? '0' + day : day].join('-');
      })(clonedDate.getFullYear(), clonedDate.getMonth(), clonedDate.getDate());
    });
    return dateStrs.join(',');
  }
  console.log(new Date('2018-05-19').datesAgo(3))
  
  