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;

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.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.