-3

I am doing a course on Udemy and I'm confused by the solution to the problem. The problem is to create a function that determines whether a year is a leap year.

The solution is:

function isLeap(year) {
    
/**************Don't change the code above****************/    
    
    //Write your code here.    
if (year % 4 === 0){

if (year % 100 === 0){

if (year % 400 === 0){

  return ("Leap year.")
}
} else {

  return ("Leap year.")

}
} else {

  return ("Not leap year.")

}
isLeap(1948);

    

/**************Don't change the code below****************/    

}

I am confused by the use of else return ("Leap year") when it is already used above for if those conditions are met. Am I missing something to do with how the code flows?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
RDA28
  • 5
  • 2

1 Answers1

0
function isLeap(year) {
    /**************Don't change the code above****************/

    //Write your code here.
    if (year % 4 === 0) {
        if (year % 100 === 0) {
            if (year % 400 === 0) {
                return "Leap year.";
            }
        } else {
            return "Leap year.";
        }
    } else {
        return "Not leap year.";
    }
    isLeap(1948);

    /**************Don't change the code below****************/
}

First of all you need to format your code properly. You can use Prettier for that. If you do this you'll notice that everything is fine with this snippet and this algorithm actually matching one presented on wikipedia.

It's worth mention that this algorithm is inefficient and if you want to read more about this here is great place to start :)

gregooroo
  • 148
  • 1
  • 9