Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Closures in Swift 2 Building Standard Library Functions Using Filter

Apply the filter function on the numbers array provided to end up with an array of odd numbers. Assign the value to a

Need Help I don't know what to do, or how to solve this questions

let numbers = [10,423,802,765,943,12,405,230,1348,128,237]let numbers = [Int](0...50)

// Enter your code below

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

let numbers = [1,2,3,4,5]
let oddNumbers = numbers.map { isOdd }

1 Answer

David Papandrew
David Papandrew
8,386 Points

You want to use filter to solve this problem. The result is a much simpler (less verbose) solution to the alternative which would be to loop through the array and append the array values that are odd to a new array.

Your function isn't that far off. But instead of creating a standalone function, you'll include the function logic in the closure for the filter method.

Here's the solution:

let numbers = [10,423,802,765,943,12,405,230,1348,128,237]

let oddValues = numbers.filter { $0 % 2 == 1 }