
Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
goldsdad Dec 30, 2016 6:18 AM (in response to Louiebo)1 of 1 people found this helpfulPost the code you've written so far, and someone will probably help get it working.

Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
Louiebo Dec 30, 2016 10:03 PM (in response to goldsdad)I solved the problem as per below image, but I have not used func number(year, isDivisibleBy: 4). So, obviously my approach was incorrect. Appreciate anyone give me the correct answer, thanks in advance.
func isLeapYear(_ year: Int) > Bool {
if year % 400 == 0 {
return true
} else if year % 100 == 0 {
return false
} else {
return year % 4 == 0
}
}
The original exercise is:
Exercise: Leap Years
To decide if a year is a leap year, there are several decisions that have to be made:
Is the year divisible by 4?
If so, is the year divisible by 100?
If not, it is a leap year.
If so, is the year divisible by 400?
If not, it is not a leap year.
If so, it is a leap year.
These decisions can be made inside a function.
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 {
if number(year, isDivisibleBy: 4) {
/
return true
} else {
return false
}
}
/
isLeapYear(2000)
/
isLeapYear(1900)
/
isLeapYear(2012)
/
isLeapYear(2017)

Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
goldsdad Dec 31, 2016 4:47 AM (in response to Louiebo)Your code, but using the number(_:isDivisibleBy:) function instead of each modulo expression would be:
func isLeapYear(_ year: Int) > Bool { if number(year, isDivisibleBy: 400) { return false } else if number(year, isDivisibleBy: 100) { return true } else { return number(year, isDivisibleBy: 4) } }
That divisibility function probably simply uses the modulo (%) operator, but there's good reason for writing and using the function: imagine you later decide to use a different technique to determine divisibility, then you could just change the internals of that function instead of changing every line of code containing a modulo expression in your original version of the leap year function.

Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
Louiebo Dec 31, 2016 7:38 PM (in response to goldsdad)Oh! Thank you very much. I am learning Xcode and Swift according to materials provided by Apple, is this the right place for me to post questions on the learning materials?

Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
aerlinn Feb 2, 2017 6:41 AM (in response to Louiebo)func isLeapYear(_ year: Int) > Bool { if number(year, isDivisibleBy: 400) { return true } else if number(year, isDivisibleBy: 100) { return false } else { return number(year, isDivisibleBy: 4) } }
There is right code, goldsdad mixed up string 0406, they should be vice versa.

Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
goldsdad Feb 2, 2017 7:31 AM (in response to aerlinn)Good catch. Thanks!

Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
Air Alarm Jun 29, 2017 4:15 AM (in response to aerlinn)Greetings, could anyone explain this solution to me please?
The task itself says:
func isLeapYear(_ year: Int) > Bool { if number(year, isDivisibleBy: 4) { // Fill in this code return true } else { return false } }
So the code should be filled in after the commented string.
As far as I understood from the task, we should start with function number(year, isDivisibleBy: 4), than 100, then 400.
The solution above does the opposite: it starts with division by 400 and ends by 4.

Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
manacus Jul 8, 2017 1:10 AM (in response to Air Alarm)Just got to this problem this morning.
This follows the order of the question:
func isLeapYear(_ year: Int) > Bool { if number(year, isDivisibleBy: 4) { if number(year, isDivisibleBy: 100) { if number(year, isDivisibleBy: 400) { return true } else { return false } } else { return true } } else { return false } }







Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
tariqfromutrecht Feb 13, 2018 4:00 PM (in response to Louiebo)Thank you for posting this! The comments on this page helped me alot. I still wanted to find out why the code was supposed to be written the way it was written in the asnwers to your question. After an hour of trying this was my answer :
func isLeapYear(_ year: Int) > Bool {
if number(year, isDivisibleBy: 4) {
if number(year, isDivisibleBy: 100) {
if number(year, isDivisibleBy: 400) {
return true
} else {
}
return false
}
}
return true
}

Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
Grimmm1914 Mar 30, 2018 3:42 PM (in response to Louiebo)Goodness this was a tough one! As I learn code one thing I’m really learning is that there are a lot of ways to get the same results. My results are the same as manacus, but I thought it would be helpful to someone else to understand why I think this is easiest. When using the ifelse argument, swift give you a few input boxes as guides. The key to making it work is not necessarily filling in the boxes in order but instead filling them in as you would read the argument. So that you are clear with “else” statement you should be filling at that time. If the answer is true then the return should be the next argument, but it was easier to skip that and instead fill in the statement for if that argument was false first, and then return back to the example of if it was true.
Hope that makes sense.

Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
bhswarthout Nov 11, 2018 9:03 AM (in response to Louiebo)I did it like this....Any comments? (I did change what they gave a bit)
func isItLeapYear(year: Int) > Bool {
if year % 4 != 0 {
print("Case No. 1; The year \(year) is not leap year because it is not evenly divisible by 4.")
return false
}
else if year % 100 != 0 {
print("Case No. 2; The year \(year) is a leap year because it is not divisible by 100.")
return true
}
else if year % 400 == 0 {
print("Case No. 3; The year \(year) is a leap year because it is evenly divisible by 400.")
return true
}
else {
print("Case No. 4; The year \(year) is not a leap year because it not evenly divisible by 400.")
return false
}
}
isItLeapYear(year: 2000) //Should be true
isItLeapYear(year: 1900) //Should be false
isItLeapYear(year: 2012) //Should be true
isItLeapYear(year: 2017) //Should be false

Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
Claude31 Nov 12, 2018 1:01 PM (in response to bhswarthout)The logic is OK, as rule for leap year is the following :
 if divisible by 4 and not divisible by 100, or
 if divisible by 400.
However, the message you give to user are not very didactic.
I would write :
func isItLeapYear(_ year: Int) > Bool { if year % 4 != 0 { print("Case No. 1; The year \(year) is not leap year because it is not evenly divisible by 4.") return false } else if year % 100 != 0 { print("Case No. 2; The year \(year) is a leap year because it is divisible by 4 and non centenal.") return true } else if year % 400 == 0 { print("Case No. 3; The year \(year) is a leap year because it is divisible by 4, is centenal divisible by 400.") return true } else { print("Case No. 4; Even though it is divisible by 4, the year \(year) is not a leap year because it is a centenal, but not divisible by 400.") return false } }
You get these messages:
Case No. 1; The year 2018 is not leap year because it is not evenly divisible by 4.
Case No. 2; The year 2020 is a leap year because it is divisible by 4 and non centenal.
Case No. 3; The year 2000 is a leap year because it is divisible by 4, is centenal and divisible by 400.
Case No. 4; Even though it is divisible by 4, the year 1900 is not a leap year because it is a centenal, but not divisible by 400.
For fun, you could also compare with today's year:
let today = Date() let calendar = Calendar.current let thisYear = calendar.component(.year, from: today) func isItLeapYear(_ year: Int) > Bool { let isVerb = thisYear >= year ? thisYear == year ? "is" : "was" : "will be" let isNotVerb = thisYear >= year ? thisYear == year ? "is not" : "was not" : "will not be" if year % 4 != 0 { print("Case No. 1; The year \(year) \(isNotVerb) leap year because it is not divisible by 4.") return false } else if year % 100 != 0 { print("Case No. 2; The year \(year) \(isVerb) a leap year because it is divisible by 4 and \(isNotVerb) a centenal.") return true } else if year % 400 == 0 { print("Case No. 3; The year \(year) \(isVerb) a leap year because it \(isVerb) a centenal and divisible by 400.") return true } else { print("Case No. 4; Even though it is divisible by 4, the year \(year) \(isNotVerb) a leap year because it \(isVerb) a centenal, but not divisible by 400.") return false } }
and get:
Case No. 1; The year 2017 was not leap year because it is not divisible by 4.
Case No. 1; The year 2018 is not leap year because it is not divisible by 4.
Case No. 1; The year 2019 will not be leap year because it is not divisible by 4.
Case No. 2; The year 2016 was a leap year because it is divisible by 4 and was not a centenal.
Case No. 2; The year 2020 will be a leap year because it is divisible by 4 and will not be a centenal.
Case No. 3; The year 2000 was a leap year because it was a centenal and divisible by 400.
Case No. 3; The year 2400 will be a leap year because it will be a centenal and divisible by 400.
Case No. 4; Even though it is divisible by 4, the year 1900 was not a leap year because it was a centenal, but not divisible by 400.
Case No. 4; Even though it is divisible by 4, the year 2100 will not be a leap year because it will be a centenal, but not divisible by 400.


Re: Leap Year Exercise in Lesson 11 (Making Decisions.Playground)
diego_developer Mar 15, 2019 12:02 PM (in response to Louiebo)This is how I made it, it is a little bit confusing at the beginning. Remember that there is already a function within the playground that tells you if that year is divisible by that number, that function is: number(_:, isDivisibleBy: )
func isLeapYear(_ year: Int) > Bool {
if number(year, isDivisibleBy: 4) {
if number(year, isDivisibleBy: 100){
if number(year, isDivisibleBy: 400){
return true
}else {
return false
}
}else {
return true
}
}else {
return false
}
}