user566029
user566029

Reputation: 45

javascript for array rotation

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;

Upvotes: 2

Views: 131

Answers (2)

Brad
Brad

Reputation: 5488

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.

Upvotes: 4

Paul
Paul

Reputation: 141829

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

Upvotes: 4

Related Questions