2

Trying to understand how the code below always brings up a number between 0-6

var dayInMili = 86400000;
var weekInMili = 604800000;
//dateTime() returns miliseconds since Thurs, Jan 1 1970, need to account for week starting Monday not Thursday.
while (currentTime.getDay() != 1){
 currentTime.setTime(currentTime.getTime() - dayInMili);
}
//we need to find the number of weeks since the beginning of the year so we can
// use that to determine schedule rotation
var weeks = Math.floor(currentTime.getTime() / weekInMili );
var startPoint = weeks % 7;
Shad
15.5k2 gold badges24 silver badges34 bronze badges
asked Jul 21, 2011 at 3:44

2 Answers 2

4

The modulo operator in the last line returns the remainder after dividing by seven. So in this case, the result assigned to startPoint is always going to be 0 - 6.

answered Jul 21, 2011 at 3:49
Sign up to request clarification or add additional context in comments.

Comments

4
startPoint = weeks % 7;

Is equivalent to taking weeks, dividing it by 7, and storing the remainder in startPoint. The remainder is always going to be between 0 and 6 for example:

7 % 7 = 0
8 % 7 = 1
14 % 7 = 0
20 % 7 = 6
answered Jul 21, 2011 at 3:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.