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

I am working on Logical Operators code challenge and my results check out in xCode but not accepted by the challenge.

This is what I am working on..is there an easier way to get the results?

var results: [Int] = []

for n in 1...100 { if (n) % 7 == 0 && !((n) % 2 == 0) { results = results + [n] } }

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

for n in 1...100 {
    // Enter your code below
    if  (n) % 7 == 0 && !((n) % 2 == 0) {
        results = results + [n]
        }
    // End code 
}

3 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Kevin Anderson,

The challenge wants you to append the values that are both multiples of 7 and odd from a range of 1 to 100 to the results array. You're on the right track, we just have to clean up a few things.

var results: [Int] = []

for n in 1...100 {
   /* We check if n is a multiple of 7 (n % 7 == 0)
   We check if odd by checking if it is not even (n % 2 != 0)
   Since we are using the AND operator, BOTH of these
   checks must be true in order to execute the statement
   inside of the block. If it is true, n is appended to the array. */
    if (n % 7 == 0) && (n % 2 != 0) {
        results.append(n)
        }
    // End code 
}

Good Luck!

Thanks Steven! Looks like I had the "!" in the incorrect place for the is not "even"

Actually looked a bit further - I received the same answer when checking results by itself, my logic was just incorrect. Thanks again.