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 trialBala Selvam
Python Development Techdegree Student 30,590 PointsApply 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
8,386 PointsYou 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 }