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

Alex Nelsen
PLUS
Alex Nelsen
Courses Plus Student 2,977 Points

What is wrong with this?

I am not understanding what is wrong with my logic here. The Array should fill with odd multiples of 7.

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

for n in 1...100 {
    // Enter your code below
    var mosA = n / 7 
    var mosB = n / 2
    if mosA % 7 == 0 && mosB % 2 > 0 { results.append (n / 7) }
    // End code 
}
Enoch Johnson
Enoch Johnson
4,899 Points

Your code create a lot of repeats. To avoid that, you should use the same variable for both operations. This way, you're checking each result against both criteria rather than creating two separate lists.

2 Answers

Nathan Tallack
Nathan Tallack
22,160 Points

Simplicity is king.

Consider my solution.

var results: [Int] = []

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

What we are doing here is evaluating if it is evenly divided by seven and it is not evenly divided by two. And then we are appending that number to the array. Note we are not dividing that number by seven before appending to the array.

Alex Nelsen
PLUS
Alex Nelsen
Courses Plus Student 2,977 Points

Thank you. I was appending n/7. I was trying variables as I was getting syntax errors. Thanks much!!!