Question: I need to add days to a date in Javascript inside of a loop.

Currently, I have this -

```
var occurences = 2;
var start_date = "10/2/2020";
for(i=0; i < occurences; i++){
  var repeat_every = 2; //repeat every number of days/weeks/months
  var last = new Date(start_date);
  var day =last.getDate() + repeat_every;
  var month=last.getMonth()+1;
  var year=last.getFullYear();
  var fulldate = month + '/' + day + '/' + year;
  console.log(fulldate);
}

```

However, this outputs 10/4/2020 twice. I know the issue is because in the 2nd iteration of the loop it again simply adds 2 to the date 10/2/2020, is there a way on the subsequent iterations I can add 2 to the previous result?

Thank you!

Answer: 
You can use a multiple of your interval and then use `last.setDate( last.getDate() + repeat_every )` to add days and get the correct month and year:

```js
var occurences = 20;
var start_date = "10/2/2020";
for(i=1; i <= occurences; i++){
  var repeat_every = 2*i; //repeat every number of days/weeks/months
  var last = new Date(start_date);
  last.setDate( last.getDate() + repeat_every );
  console.log( `${last.getDate()}/${last.getMonth()+1}/${last.getFullYear()}` );
}
```
Make counter `i` a multiple of `repeat_every`:

```js
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
var occurences = 2;
var start_date = "10/2/2020";
for(i=1; i <= occurences; i++){
  var repeat_every = 2*i; //repeat every number of days/weeks/months
  var last = new Date(start_date);
  var day =last.getDate() + repeat_every;
  var month=last.getMonth()+1;
  var year=last.getFullYear();
  var fulldate = month + '/' + day + '/' + year;
  console.log(fulldate);
}
```

```html
<!-- https://meta.stackoverflow.com/a/375985/ -->    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
```