JavaScript: Storing dates in array in for loop -
i'm trying loop through check if first date less second date. if is, want store value in array, add 1 day first date, go through process. think that's i've written, i'm not sure why it's not working. when console out any of array values, it's last date appears , can't of other values store. after stepping through firebug, looks being overwritten. i'm using moment.js dates.
var counter = 1; var arr = []; var = firstdate.todate(); for(counter; firstdate < seconddate; counter++){ arr[i]=firstdate.todate(); firstdate.add(1, "day"); } console.log(arr[2]);
any appreciated!
the variable i
never changes once set, you're writing same index.
also, counter
variable never used. consider using counter
variable index unique each iteration. alternatively, if want continue using date index, add line i = firstdate.todate();
top of loop.
for latter, loop should this:
var counter = 1; var arr = []; var = firstdate.todate(); for(counter; firstdate < seconddate; counter++){ = firstdate.todate(); arr[i]=firstdate.todate(); console.log(firstdate.todate()); firstdate.add(1, "day"); } console.log(arr[2]);
i suggest adding console log loop (as above) if you're having problems, see dates being added being added.
Comments
Post a Comment