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

Matt Frerichs
Matt Frerichs
4,015 Points

Need help creating oddNumbers array in closures challenge.

Hi Everybody,

I am having difficulty creating the oddNumbers array in the second step of the following code challenge:

Given an array of integers, called numbers, use the filter function (and the helper function we created) to create a new array containing only odd numbers. Assign this new array to a constant named oddNumbers.

When the isOdd Function returns the boolean values, how do I convert them to Integer values for the new array? Does it automatically return integers because it filters the numbers array?

I need help completing this challenge! Thanks for the help.

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

// Enter your code below

func isOdd(i: Int) -> Bool {
  return i % 3 == 0
}

let oddNumbers = numbers.filter(Int, isOdd)

3 Answers

Hey Matt, I think the problem came when you completed the first challenge wrong. The isOdd function should "return i % 2 != 0" instead of " return i % 3 == 0"

And to pass the last test, you need to pass in the function isOdd as a parameter to filter, therefore "Int" can be removed from your code.

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

let oddNumbers = numbers.filter(isOdd)       

Should work now :D

Matt Frerichs
Matt Frerichs
4,015 Points

Thanks for the help Gianni!

Matt Frerichs
Matt Frerichs
4,015 Points

Does the Bang "!" operator unwrap the boolean values to return integers in the array?

let numbers = [10,423,802,765,943,12,405,230,1348,128,237] let oddValues = numbers.filter {$0%2 != 0}

Hi Matt, good to know it worked for you.

The reason I'm using "i % 2 != 0" is to say any number divided by 2 isn't equal to 0, which in other terms means it's an odd number. Using "i % 3 == 0 " may work for some odd numbers however it is not consistent across the entire array and will create mistakes. A quick example

let array = [1,2,3,4,5,6,7,8,9]

let filteredArray = array.filter { $0 % 3 == 0}

filteredArray // -> 3,6,9 (and number 1 is missing)

Check this link (https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/SwiftStandardLibraryReference/Array.html) Very useful to understand the array function in Swift.

Matt Frerichs
Matt Frerichs
4,015 Points

Thanks, I'll brush up on that.

Thanks for the feedback John DiPanfilo :) . Glad I could help. Have a good day