Thursday, July 15, 2021

Junior Developer Central JS Practice Exercise - 16

 
Question: 

Given a year, report if it is a leap year. 




Answer: 

function isLeapYear (year) {

const leapYear01 = year; 
const leapYear02 = leapYear01 / 4;

if (Number.isInteger(leapYear02)) {
return true; 
} else { 
return false;
}
}




> we declare a function 'isLeapYear' 
> it has one parameter 'year' 
> leap year has 366 days
> leap year comes every 4 years
> all leap years are divisible by 4
> hence all the years that are divisible by 4 are 'leap years' 
> so first we create a variable 'leapYear01' 
> we initialize it with a value that we get in 'year' argument
> then we create another variable 'leapYear02' 
we initialize it with a value that we get by dividing the 'leapYear01' by 4
> we then open an if/else statement
> we want to check if the value stored in 'leapYear02' is an integer
> an integer is a whole number
> an integer is a number that can be written without a fractional component
> our if statement will check if the value is an integer
> if it is an integer, then we want to return true
> if it is not an integer, then we want to return false
> we return false as the value stored is not a whole number and so the value stored in 'leapYear02' is not divisible by 4
> if it is not divisible by 4 then it is not a 'leap year' 

No comments:

Post a Comment