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 Closures in Swift Building Standard Library Functions Using Filter

Randell Pur
Randell Pur
4,934 Points

I could use some help please

I am not sure what I am doing wrong. I watched the video that covers this a few times but I am missing something and I can not figure it out. Please help and give some details on what I am doing wrong.

Thank you!

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

let oddValues = numbers.filter { $0 % 3 == 0}
Randell Pur
Randell Pur
4,934 Points

I figure it out. I really do not know how to explain it but this is the code that worked for me.

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

let oddValues = numbers

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Sorry, was typing my reply as you figured it out.

It's great you figured it out. :thumbsup:

Just another hint... You can keep your original code as is... just need to change the .. % 3 == 0 to what you have below. It's more DRY that way.

Nice work!!

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi Randell Pur

You're on the right track. If you have a look at the hint the challenge is giving you when you check the code:

Hint: You need to filter out the even numbers

Right now you are checking if the number is divisible by 3. Not sure why? You are using the correct operator (the modulo), but to check if a number is even you need to make sure the % 2 equals zero. With this in mind, you are adding the odd numbers, so you need to check that % 0 does not equal zero (so all the odd numbers).

Give it another go with this in mind.

Keep Coding! :) :dizzy:

Randell Pur
Randell Pur
4,934 Points

Thank you for the response. I was getting confused after reading some articles online and I started combining things that weren't needed nor fit the assignment. I have been studying for about 12 hours now and I needed a break, came back and laughed at myself because it was right in front of my face the whole time.

Thank you for the great explanation also! I know it will help others stuck on this problem as well!