+ 2

# Leap Years

Why is it not working? For example if I put in the number 4 and I press convert it will come out as Not A Leap Year? Any solution to fix this problem? https://code.sololearn.com/WYG87Ior0jm4/?ref=app

21 Answers

+ 5

Condition is
if year%4==0 and year%100 != 0
year is a leap year
else if year%400==0
year is a leap year
else
year is not a leap year

+ 4

1900 is divisible by 4 and 100 but it is not a leap year.

+ 2

Your if is wrong, it should be more like this:
if (x % 4 == 0 || x % 400 == 0)

+ 2

OHH NEVERMIND I GET IT NOW

+ 1

Why can it nake it so (yr % 100 == 0) {
document.getElementById…”Not Leap Year”
} unless yoi csn do do that

+ 1

Here this is what i mean:
https://code.sololearn.com/WYG87Ior0jm4/?ref=app Jayakrishna🇮🇳

+ 1

You see?

+ 1

Years multiple of 100 are
century years 1000,1100,1200, .., 1900,2000,.. are divisible by 4 and 100 but all not leap years
1900 is divisible by 100 but not leap year.
2000 is divisible by 100 but leap year.
Using || (or) operator result true if any one condition is true, no need both.
Leap Year must be divisible by 4 but not divisible by 100
Or
Leap Year must divisible by 400

+ 1

Hmm? But my document says Not A Leap Year?

+ 1

No. It says leap year for 1900
simple it should be divisible of 4 and not divisible by 100 . otherwise divisible by 4 and 400...
Else cases not a leap year..

+ 1

Ik 1900 isnt a leap year but every 100 yrs i want my code to say “Not a Leap Year” abd thats what i did but it didnt work

+ 1

Make changes as I mentioned in my first post. It works. Otherwise don't work.

+ 1

Ok

+ 1

I think im starting to get it jow tho

+ 1

So thanks

0

Okay, so yr % 100 != 0, what is the poibt of the 0?

0

Still not get it? See this,
https://www.almanac.com/content/when-next-leap-year
You're welcome..

0

Your Mom
you can condense your code to:
function Leap_Year() {
document.getElementById("demo").innerHTML = !((yr % 4) || (!(yr % 100) && yr % 400)) ? "Leap Year": "Not a Leap Year"; }
Or use Date, and check if February have 29
https://code.sololearn.com/WslBGD5wir7k/?ref=app

- 1

const leap_year = year%4&&year100!=0?year%400?yes:yes:no

- 1

Can anyone create new code for me
Because my has so many errors