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

Joseph Maza
Joseph Maza
9,184 Points

Control Flow Extra Credit

Hi everyone, I was doing the extra credit for this section in Swift basics. I think I have the basic part of it down, but I want it to show numbers in front of the odds and evens. Here's what I have so far thank you for your time.

// Even or Odd to 100

import UIKit

var numbers = 1...100

for number in numbers{ if ((number) % 2 == 0){ println("even") } else if ((number) % 2 != 0){ println("odd") }else { println(number) } }

You can use string interpolation to add the number before Odd or Even. Also you have extra code there that isn't needed. Here is the solution I found:

var numbers = 1...100

for number in numbers { if ((number) % 2 ==0){ println("(number) is even") }else{ println("(number) is odd") } }

I hope this helps!

Joseph Maza
Joseph Maza
9,184 Points

Yes, absolutely helped thank you Danielle Aristizabal!

1 Answer

George Steiner
George Steiner
587 Points

You could even skip declaring your variable and just write

for i in 1...100 { }