2

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.

1
  • 1
    It's happening because you have a for loop that's iterating over date objects, and i is 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. Commented Oct 14, 2015 at 21:09

2 Answers 2

1

You need to create a new date object inside the loop. When using .push() on an array in javascript, the objects and arrays are pushed by reference. Built-in types like numbers are pushed as a copy.

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);
        var m = new Date(i);
        a.push(m);

        console.log(a[j++]);
    }

}


for (var k = 0; k <= a.length; k++) {

    console.log(a[k])
}

Sign up to request clarification or add additional context in comments.

2 Comments

console.log(a[j++]); give the "correct " date, because as all the items of a are references to i, it displays the last value of i
and for the same reason all the values are "jan 9 2012", which it is the last value assigned to i before exit the for loop.
1

You create a date object at the start of the loop and store a reference to it in i.

Each time you go around the loop, you copy that reference into the array and modify the value of the date object.

You end up with an array containing a bunch of references to the same date object which is set to the final value you set it to.

You need to create a new date object each time you go around the loop.

e.g.

a.push(new Date(i));

4 Comments

On line 16, why does "console.log(a[j++]);" give the correct dates?
okay, that given solution worked but I am still unclear as to why the date being stored in array "a" is "jan 9 2012" my end date according to code should be maximum "jan 06 2012"
Because "jan 9 2012" is the last value assigned to i before exit the for loop
@Ketcomp — console.log(a[j++]); works because you are testing inside the original loop, so you are testing before you assign the new value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.