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

How do I send my 'If' statement results to the 'results' variable?

I believe I have some if not most of the challenge close to completion but I can't seem to work out how to send the results from each loop back to the variable called results. Please help!

2 Answers

Marina Alenskaja
Marina Alenskaja
9,320 Points

Hi again

So, I used the append method instead:

var results: [Int] = []

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

Also, you should always write if n then ... (in the code above you are starting with 2%n) If that makes any sense :-)

Ohhhh I see what you mean. You've cleared up a lot of confusion, thanks for the help!

Marina Alenskaja
Marina Alenskaja
9,320 Points

Use the += operator to add the number to the array if all conditions are true: results += n

I tried but it keeps coming up with an error asying "Binary operator += cannot be applied to operands of type [Int] and Int"

Marina Alenskaja
Marina Alenskaja
9,320 Points

Can you please copy and paste your code in? Then I can see what's missing :-)

Sorry heres the code I have put in

var results: [Int] = []

for n in 1...100 {

    if (2%n != 1) && (7/n != 0) {
  results += n
}

}