From: John W Higgins Date: 2013-03-29T05:48:38+09:00 Subject: Re: Chris Pine Leap Year --089e0160c9f2533be904d902471f Content-Type: text/plain; charset=ISO-8859-1 On Thu, Mar 28, 2013 at 1:17 PM, Phil H. wrote: > This one is stumping me because of all the variables needed. Only thing > taught before this challenge was basic branches and loops. This is what > I have so far. > > puts 'Leap Year Calculator' > puts ' ' > puts 'Enter the starting year.' > starting_year = gets.chomp > puts 'Enter the ending year.' > ending_year = gets.chomp > > The rest doesn't work at all because if you put something like 2096-2104 you would print out 2096, 2100 which is incorrect in two ways 1) 2100 isn't a leap year 2) 2104 should be printed and your second while clause precludes that You shouldn't be worrying about the starting year as much as you are - you need to handle every leap year in the same fashion So lets try something like this leap_year = starting_year + starting_year % 4 #set yourself onto a possible leap year to start with while leap_year <= ending_year if (leap_year % 100) != 0 || (leap_year % 400) == 0 puts leap_year end leap_year = leap_year + 4 end And if for some reason you have a problem with the or statement ("||") you can use this instead if (leap_year % 100) != 0 puts leap_year elsif (leap_year % 400) == 0 puts leap_year end John --089e0160c9f2533be904d902471f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Thu, Mar 28, 2013 at 1:17 PM, Phil H.= <lists@ruby-forum.com> wrote:<= br>
This one is stumping me because of all the variables needed. Only thing
taught before this challenge was basic branches and loops. This is what
I have so far.

puts 'Leap Year Calculator'
puts ' '
puts 'Enter the starting year.'
starting_year =3D gets.chomp
puts 'Enter the ending year.'
ending_year =3D gets.chomp


The rest doesn't work at all because if you p= ut something like 2096-2104 you would print out 2096, 2100 which is incorre= ct in two ways

1) 2100 isn't a leap year
2) 2104 should be p= rinted and your second while clause precludes that

You shouldn't be worrying about the starting year as much as you ar= e - you need to handle every leap year in the same fashion

So lets t= ry something like this

leap_year =3D starting_year + starting_year %= 4 #set yourself onto a possible leap year to start with

while leap_year <=3D ending_year
=A0 if (leap_year % 100) !=3D 0 = || (leap_year % 400) =3D=3D 0
=A0 =A0 puts leap_year
=A0 end
=A0 l= eap_year =3D leap_year + 4
end

And if for some reason you have a = problem with the or statement ("||") you can use this instead

if (leap_year % 100) !=3D 0
=A0 puts leap_year
elsif (leap_year %= 400) =3D=3D 0
=A0 puts leap_year
end

John
--089e0160c9f2533be904d902471f--