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

Cyre Simard
Cyre Simard
794 Points

Why isn't this logical?

I'm trying to work through this, have watched again the relevant segments, but this is say that it isn't logical....but it contains both a for in loop and an if statement. Do I need to make a statement that says else do nothing? If so, what is swift for "do nothing"?

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

for n in 1...100 {
    // Enter your code below
    if n != even && n % 7 = 0 {
    results.append
   }
    // End code 
}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Swift doesn't know what "even" is. We do however. An even number is any number (x) when operated with the modulus operator % and a 2 returns a 0. IE 4 % 2 == 0. So 4 is even. Also use the double equals when comparing values. Also, you tell it to append, but you never say what to append. We're appending the value of n. Here's my code to clarify:

var results: [Int] = []

for n in 1...100 {
    // Enter your code below
    if(n % 2 != 0 && n % 7 == 0) {  //n%2 != 0 means it's odd which is what the challenge asks for
      results.append(n)
    }
    // End code 
}

``
Cyre Simard
Cyre Simard
794 Points

I think all that made sense. Like, it did to me, but the code challenge error seemed like it was having a problem with my answer other than what you've explained (rather well, btw). Thanks.