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

Jeremy Powell
Jeremy Powell
2,420 Points

Swift Closures: Using Filter challenge task 1 of 2 is wrong how?

"Challenge Task #1: Define a function, named isOdd, to check if a number is odd. The function takes a number and returns either true or false depending on if the number is odd or not."

I know I am reading this wrong as the function "[inserts] a number and returns [using a return bool] with true or false." I am confused as to how I insert the constant "numbers" to return a bool, which I think is what the question is asking for. Thanks!

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

func isOdd() -> Bool {
    return i % 2 == 0
}

let oddNumber = numbers.filter(isOdd)

2 Answers

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points
// Check your syntax!
// Your checking if a number is odd and returning true if correct.

func isOdd(i:Int) -> Bool {

return i % 2 != 0

}

isOdd(3)     // True
isOdd(2)     // False
Jeremy Powell
Jeremy Powell
2,420 Points

Thanks so much! I did have the "i" in the return question, don't know why it had changed to an integer when I posted the question. It needed to be force unwrapped with the "!=" which did it. Thanks again for the quick response Chris!

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points

!= is not unwrapping an optional.

return i % 2 != 0

The function is stating if I take a number and divide it by two is the remainder not equal to 0. If this is the case return true other wise return false.

if we wanted to check for even number...

return i % 2 == 0