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 trial

iOS Swift Closures Closures and Closure Expressions Using Filter

Cody Adkins
Cody Adkins
7,260 Points

Help with my code

Not sure what I am doing wrong here...

filter.swift
let numbers = [Int](0...50)

// Enter your code below

func isOdd(number: Int) -> Bool {
return number % 2 == 0 
}

2 Answers

Paul Brazell
Paul Brazell
14,371 Points

You are not returning an appropriate return type of Bool. You need to check if the number is Odd. If it is, then you return true. Else return false.

let numbers = [Int](0...50)

// Enter your code below

func isOdd(number: Int) -> Bool {
if number % 2 != 0 {
return true
}
else {
return false
}
}

Paul's right. But here's an opportunity to practice what some call Boolean Zen:

func isOdd(n: Int) -> Bool {
  return n % 2 != 0 
}

Since n % 2 != 0 is going to be either true or false for any n, just return it, rather than returning true or false in an if... else...

Paul Brazell
Paul Brazell
14,371 Points

Great tip! Will be sure to remember this in the future!