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

Using Filter challenge [Closuers]

I don't know why I keep getting an error saying that I need to make sure I am using the filter function and assigning the odd numbers to a constant called oddNumbers which I think is what I am exactly doing.

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

// Enter your code below
func isOdd(Int) -> Bool {
  let oddNumbers += numbers.filter({$0 % 2 != 0})
}

1 Answer

I guess they want you to use a closure but you can pass it without it since they don't specifically ask for one.

Set a function isOdd that takes an Int and return the odd numbers. Use the filter function to create a new array containing only odd numbers. Assign this new array to a constant named oddNumbers.

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

let oddNumbers = numbers.filter(isOdd)