// Divide by 7 to get weeks until Xmas, use modulus to get remaining days // Use floor() to round down to even numbers weeksToXmas = Math.floor(daysToXmas / 7); daysToXmas = Math.floor(daysToXmas % 7);
It is important to mentally track what's going on in a program. The variable daysToXmas now holds the number of days until the next Christmas. Nobody wants to think that Christmas is over 100 days away, so the code is going to convert the number of days into the number of weeks and the remaining days.
To convert to weeks, you have to divide the total number of days by 7. However, saying that there are 5.23493 weeks until Christmas is not the effect that we are after. The Math object has a method called floor() which takes a floating point value as an argument and rounds down to the next greatest integer. The expression, daysToXmas/7 results in a numeric value, which is then processed by the floor() function, conceptually the same as assigning that value to a variable using the assignment operator (=).
To get the remaining days, you have to use the modulus operator (%). The modulus returns the whole number remainder from a division. In this example, it is going to divide by seven, and then return the 'left over days'.
It is important that the steps be carried out in the order above, or that a temporary variable be used. Because the conversion to weeks requires that daysToXmas hold the total number of days until next Christmas, converting the remaining days first would render daysToXmas useless for calculating the number of weeks.