Can someone explain to me how this works?

# Thread: Can someone explain to me how this works?

1. Junior Member
Join Date
Dec 1969
Posts
7

## Can someone explain to me how this works?

Hi again :)<BR><BR>Thanks to Paul for helping me earlier. I&#039;ve got another quick question. Again it&#039;s the wrong forum but I really need a quick answer if that&#039;s possible. I&#039;ve seen this function for finding out if a year is a leap year or not but I don&#039;t fully understand it.<BR><BR>Here it is:<BR><BR>function leapYear(year) {<BR> if (year % 4 == 0) {<BR> return true;<BR> } else {<BR> return false;<BR> }<BR>}<BR><BR>The result is then assigned like this:<BR><BR>var isLeap = leapyear(year) ? 29 : 28 //number of days in February<BR><BR>Surely this isn&#039;t right because the modeul operator determines the remainder of an expression, so if you divide 366 by 4, you do get a remainder. Is this just me or have I fallen out the wrong side of the bed today? *confused look*<BR><BR>Thanks!<BR><BR>Daria.

2. Senior Member
Join Date
Dec 1969
Posts
1,274

## Sure!

&#062;Here it is: <BR><BR>&#062;function leapYear(year) { <BR>&#062; if (year % 4 == 0) { <BR>&#062; return true; <BR>&#062; } else { <BR>&#062; return false; <BR>&#062; } <BR>&#062;} <BR><BR>You&#039;re actually passing a YEAR to the function, like:<BR>leapyear(2001) or leapyear(1996). THIS value is divided by 4, and if the remainder is 0, guess what? It&#039;s a leap year! :)<BR><BR>&#062;The result is then assigned like this: <BR>&#062;<BR>&#062;var isLeap = leapyear(year) ? 29 : 28 //number of days in &#062;February <BR><BR>If leapyear(year) returns true, 29 is assigned to isLeap, otherwise, isLeap becomes 28.<BR><BR>Got it? Good :)

3. Junior Member
Join Date
Dec 1969
Posts
7

## RE: Sure!

Ah I see!!! For some stupid reason I had it in my head it was going to divide the number of years by 4, which gave 91.5. Hehe, I thought it should&#039;ve been 3 instead as that divides into 366 but not 365! Oh well, figured out a different version anyways :)<BR><BR>Thanks again :)<BR><BR>Daria.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•