Swift Exercise: Leap Years

Hello! I can't understand what am I supposed to do in this exercise. Can someone walk me through?

Exercise: Leap Years

To decide if a year is a leap year, there are several decisions that have to be made in the correct order.

Is the year divisible by 4?

If so, is the year divisible by 100?

If so, is the year divisible by 400?

If so, it is a leap year.

If not, it is not a leap year.

If it's not divisible by 100, it is a leap year.

If it's not divisible by 4, it is not a leap year.

These decisions can be made inside a function as a series of nested if...else statements.

The number(_:, isDivisibleBy:) function has been built into this playground to make this exercise easier. Below is an incomplete function for deciding if a given year is a leap year:

func isLeapYear(_ year: Int) -> Bool {

    // Is the year divisible by 4?

    if number(year, isDivisibleBy: 4) {

        return true

    } else {

        return false

    

}







// Should be true

isLeapYear(2000)

// Should be false

isLeapYear(1900)

// Should be true

isLeapYear(2012)

// Should be false

isLeapYear(2017)

Exercise Complete the function above so that the rules are all followed and the examples get the correct answers. 

Hint: Try using the rules as pseudocode by making them into comments. Then write the code that goes with each comment underneath it.

Exercise For a challenge, complete the function below. It should behave identically without using any nested conditional statements. Use a single level of if/else statements along with conditionals that use Boolean operators. 

Hint: Create constants that represent the three key conditions, and then compose a Boolean expression with those constants.

func isLeapYear2(_ year: Int) -> Bool {

}

Answered by Claude31 in 731519022

You start here:

func isLeapYear(_ year: Int) -> Bool {

    // Is the year divisible by 4?
    if number(year, isDivisibleBy: 4) {
        return true
    } else {
        return false
   }

}

.

This is what they instruct to do.

Hint: Try using the rules as pseudocode by making them into comments. Then write the code that goes with each comment underneath it.

Is the year divisible by 4?
If so, is the year divisible by 100?
If so, is the year divisible by 400?
If so, it is a leap year.
If not, it is not a leap year.
If it's not divisible by 100, it is a leap year.
If it's not divisible by 4, it is not a leap year.
func isLeapYear(_ year: Int) -> Bool {

    // Is the year divisible by 4?
    if number(year, isDivisibleBy: 4) {
       // If so, is the year divisible by 100?
       // If it's not divisible by 100, it is a leap year.
       // If so, is the year divisible by 400?
       // If so, it is a leap year.
        return true
      // If not, it is not a leap year.
    } else {
        return false
   }

}

And now the code:

func isLeapYear(_ year: Int) -> Bool {

    // Is the year divisible by 4?
    if number(year, isDivisibleBy: 4) {
       // If so, is the year divisible by 100?
        if number(year, isDivisibleBy: 100) {
            // If so, is the year divisible by 400?
            if number(year, isDivisibleBy: 400) {
                // If so, it is a leap year.
                return true
            } else {
                // If not, it is not a leap year.
                return false
            }
        } else {
            // If it's not divisible by 100, it is a leap year.
            return true
        }
    } else {
        return false
   }

}

Note: I posted a detailed answer with explanation of the result here:https://developer.apple.com/forums/thread/70204

Accepted Answer

You start here:

func isLeapYear(_ year: Int) -> Bool {

    // Is the year divisible by 4?
    if number(year, isDivisibleBy: 4) {
        return true
    } else {
        return false
   }

}

.

This is what they instruct to do.

Hint: Try using the rules as pseudocode by making them into comments. Then write the code that goes with each comment underneath it.

Is the year divisible by 4?
If so, is the year divisible by 100?
If so, is the year divisible by 400?
If so, it is a leap year.
If not, it is not a leap year.
If it's not divisible by 100, it is a leap year.
If it's not divisible by 4, it is not a leap year.
func isLeapYear(_ year: Int) -> Bool {

    // Is the year divisible by 4?
    if number(year, isDivisibleBy: 4) {
       // If so, is the year divisible by 100?
       // If it's not divisible by 100, it is a leap year.
       // If so, is the year divisible by 400?
       // If so, it is a leap year.
        return true
      // If not, it is not a leap year.
    } else {
        return false
   }

}

And now the code:

func isLeapYear(_ year: Int) -> Bool {

    // Is the year divisible by 4?
    if number(year, isDivisibleBy: 4) {
       // If so, is the year divisible by 100?
        if number(year, isDivisibleBy: 100) {
            // If so, is the year divisible by 400?
            if number(year, isDivisibleBy: 400) {
                // If so, it is a leap year.
                return true
            } else {
                // If not, it is not a leap year.
                return false
            }
        } else {
            // If it's not divisible by 100, it is a leap year.
            return true
        }
    } else {
        return false
   }

}

Note: I posted a detailed answer with explanation of the result here:https://developer.apple.com/forums/thread/70204

Thank you for your help!😃

Swift Exercise: Leap Years
 
 
Q