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
2 Answers 2
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
Brad
5,4681 gold badge37 silver badges56 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Paul
142k28 gold badges285 silver badges272 bronze badges
Comments
lang-js