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

Need help figuring out what's wrong with my code

I’m having difficulty figuring out why my code isn’t being accepted as working. It runs fine in Playground and after checking the values produced in the array, it seems to be correct. Am I missing something? We are supposed to figure out which values from 1 to 100 are divisible by 7 but also odd.

Here is what I have:

var results: [Int] = []

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

3 Answers

Hi Joanna,

It is objecting to += being used to add an Int to an [Int] array. Using append() on results avoids this:

var results: [Int] = []

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

Steve.

Hi Steve! Thanks for the response. In playground, += is a valid way to add items to an array, but it does require the brackets. He also mentioned this way in the course along with the append statement. I switched to using the append statement in my code and it was accepted as correct! I'm curious why the += wasn't accepted as correct, since it's a valid way to do it, but oh well! Thanks for the help Steve! :) Joanna

I agree - I've used it that way before many times ... I'm doing some digging to see what the issue may be.

OK - I think the 'issue' is that the += operator is a way of adding an array to an array, not an individual element. So, you required the square brackets because you added a single-element array to the results array. Strong typing means you can't have different things on either side of the binary operator +=, which I get. So you've converted your Int to an [Int] and the results (no pun intended) look identical. I'm wondering if there's some semantic difference between [[7], [21], [35], [49]] (etc. - I got bored there), and [7, 21, 35, 49] ... the compiler in this challenge thinks so, but I can't track down the difference in Xcode.

Yes, it seems this changed so that .append() is used for single items, and += works for arrays. What the distinction is, as in the above comment, between [[7]] and [7], I don't know.

A few threads on this point:

http://stackoverflow.com/questions/24002733/add-an-element-to-an-array-in-swift

https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=swift%20difference%20between%20%2B%3D%20and%20.append()

Good insight! I see what you're saying, an array of one-item arrays, and needing the same type on either side of the +=. Totally understanding that now. I was just doing it because it seemed more simple to do it that way, but I guess in the grand scheme of things, an array of one-item arrays may seem a bit more complicated than an array of integers. hehe. Thanks for the links, too!

:+1: