Constant Contact Challenge Fiddle

by gkohen

JavaScript

//New 

// Print 100 Wednesdays from the start of this month
var oneDay=1000*60*60*24,max = 101, currDay=new Date(new Date().setDate(1)).getTime();
while (max--) {
    var secondWed=(((10-new Date(currDay=new Date(currDay).setDate(1)).getDay())%7)+7)*oneDay+currDay;
    console.log(new Date(secondWed));
    currDay=new Date(currDay+(32*oneDay)).setDate(1);
}

//Old
// Print 100 Wednesdays from the start of this month
var oneDay=1000*60*60*24, oneWeek=oneDay*7,max = 100,maxStart=max;
var currDay=new Date(new Date().setDate(1)).getTime();
var currMonth=new Date(currDay).getMonth();
var currWed=0;
var step=oneDay;
while (max) {
    if (currMonth!=new Date(currDay).getMonth()) {
        currWed=0;
        currMonth=new Date(currDay).getMonth();
    }
    var date=new Date(currDay);
    currWed+=(date.getDay()==3)?((step=oneWeek)&&1):0;
    if  (currWed==1) {
        max--;
        console.log(new Date(currDay));        
    }
    currDay+=step;
}