0

I am currently getting weird behavior from the following while loop in javascript.

var i = 0;
var endDate = new Date();
var startDate = new Date();
startDate.setDate(startDate.getDate() - 6);

while (start <= end){
  alert (start.getDate());
  start.setDate(start.getDate() + i);
  i++;
}

The output I would expect (assuming today is Sept. 26) would be :

20 21 22 23 24 25 26

However I get the following output

20 20 21 23 26

I don't think I am handling the scope of the startDate variable correctly but I am not sure how to fix this.

Any help is greatly appreciated

3
  • 1
    You're setting the date, which modifies it. On top of that, you're adding i to the date, which is incrementing. As such, the date that is set increases from the previous sets and the current i. Either create a date that remains unmodified, and add i to that, or just add 1 instead of i. jsfiddle.net/gnmvao4n Commented Sep 26, 2014 at 23:00
  • What are start and end? Commented Sep 26, 2014 at 23:01
  • I assume start and end are really just typos for startDate and endDate Commented Sep 26, 2014 at 23:02

3 Answers 3

1

Assuming start and end are your date variables:

while (start <= end){
  alert (start.getDate());
  start.setDate(start.getDate() + i);
  i++;
}

Loop iteration 1: date is 20 and you add 0 => 20

Loop iteration 2: date is 20 and you add 1 => 21

Loop iteration 3: date is 21 and you add 2 => 23

Loop iteration 4: date is 23 and you add 3 => 26

To get what you expect, try this:

while (start <= end){
  alert (start.getDate());
  start.setDate(start.getDate() + 1);
  i++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. Everybody basically gave the same answer but I think yours was the clearest.
1

Your iterator is not correct. You don't really even need an iterator. Just add 1 instead of i to you second line of the while loop and delete var i = 0 and i++.

var endDate = new Date();
var startDate = new Date();
startDate.setDate(startDate.getDate() - 6);

while (startDate <= endData){
  alert (start.getDate());
  start.setDate(start.getDate() + 1);
}

The reason why you are getting 20 20 21 23 26 is because on the first loop you are six days back, and add zero; the second loop you are six days back, and add one; the third loop you are five days back and add 2; the fourth loop you are now three days back, and adding 3; etc.

Comments

0

If you debug through your code, you'll likely see each iteration of the loop you're modifying the date by a different value - i is changing each time through the loop. First time it updates the date by 0, then by 1, then 2, etc. That's what you're seeing in the output.

Replace i with 1 and you'll get your expected output. In fact, you don't even need i.

Comments

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.