Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjurgen meco
30 Pointswhat should i do, please help :(
i though i go it right, please help
func isDivisible(#dividend: Int, #divisor: Int) -> Bool {
if dividend % divisor == 0 {
return true
} else {
return false
}
}
func isNotDivisible(#dividend: Int, #divisor: Int) -> Bool {
if dividend % divisor != 0 {
} else {
return false
}
}
2 Answers
Steve Hunter
57,712 PointsThere's an answer here but I think (know) that code can be improved too. There's no need for an if
statement to detail a return true or false; just return the comparison.
Maybe something like:
func isDivisible(#dividend: Int, #divisor: Int) -> Bool {
return dividend % divisor == 0
}
Then negate that in another function:
func isNotDivisible(#dividend: Int, #divisor: Int) -> Bool {
return !isDivisible(dividend: dividend, divisor: divisor)
}
Make sense?
Steve.
Tosin Peters
9,541 PointsThere is nothing wrong with your code. The only thing you are missing is the return true statement in second function.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsThe key thing here is not to repeat the original code - the function provided, while being overly wordy, does a job. The new method wants to do exactly the opposite of that job. Don't write the method out again; call the first method and negate it with the
NOT
operator. Be DRY, in other words.So there's no need for
if
` statements in the second method (or the first, in reality) - just call the first method from inside the second and return the negated value it passes back.