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 2.0 Collections and Control Flow Control Flow With Conditional Statements Working with Logical Operators

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

won't compile says that I need a "{ " after the if statement. var results: [Int] = [] for n in 1...100 { if !

logicalOperators.swift
var results: [Int] = []

for n in 1...100 {
    if !"not even" && n %7 == 0 {
       results.append(n)
    }

}

2 Answers

Hey Ingo,

The Challenge is asking you to find which of the numbers between 1 and 100 are, both, odd and a multiple of 7. I totally get why you tried to check for oddness using the string "not even" — because of the Challenge's "hint" — but, unfortunately, Swift does not have such functionality. Instead, to see if a number is odd, you have to divide the number by 2 and see if there is any remainder. If there is a remainder, then the number is odd; if there isn't, then the number is even. Fortunately, Swift offers us an easy method of finding the remainder of a given division between two numbers: the modulo operator (%). Let me know if you need more help.

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

var results: [Int] = []

for n in 1...100 { if n %2 == 1 && n %7 == 0 { results.append(n) }

}

the compiler tips are hard to read . Xamarin is much more readable. I now get the following exceptions:

swift_lint.swift:6:10: error: expected '{' after 'if' condition if n %2 == 1 && n %7 == 0 { ^ swift_lint.swift:6:31: error: braced block of statements is an unused closure if n %2 == 1 && n %7 == 0 { ^ swift_lint.swift:6:31: error: expression resolves to an unused function if n %2 == 1 && n %7 == 0 { ^

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

AH HA! Got it! Stacker Exchange example had this syntax format:
if (test % 3 == 0)

I copied the format and it compiled Thank you