Following is my JavaScript code:
var enddate, finaldate, startyear, endyear, i;
startyear = 2010;
endyear = 2011;
enddate = new Date(endyear, 11, 31)
finaldate = new Date(enddate)
var a = [];
j = 0;
enddate.setDate(enddate.getDate() + 6)
for (i = new Date(startyear, 0, 1); i <= enddate; i.setDate(i.getDate() + 6)) {
if (i > finaldate) {
console.log(finaldate);
} else {
console.log("Value of i: " + i);
a.push(i);
console.log(a[j++]);
}
}
for (var k = 0; k <= a.length; k++) {
console.log(a[k])
}
The statement console.log("Value of i: " +i); prints the correct values in console but when I try printing the values of an array a at the end, it gives me ALL the values as Mon Jan 09 2012 00:00:00 GMT-0500 (EST), cannot understand why would this happen.
iis a global reference to an object that you keep changing. To be clearer, you're not pushing unique values to the array, but the same reference.