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 Collections and Control Flow Control Flow With Conditional Statements Working With Logical Operators

Miguel Chavez
Miguel Chavez
2,128 Points

I dont know why my answer is not right

Here is my code

The result of the code is [7, 21, 35, 49, 63, 77, 91]

I am missing something?

Thanks in advance!

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

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

2 Answers

andren
andren
28,558 Points

Your code is perfectly fine, the issue is that the code checker for this task is quite picky. It expects you to use the append method to add numbers to the array rather than using concatenation like you are doing.

The resulting array will be the same either way, but unless the code checker sees the append method being used it won't pass the code.

Here is your solution modified so it will pass the challenge:

var results: [Int] = []

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

why do we need the open and close curly braces before "results.append (n)

Miguel Chavez
Miguel Chavez
2,128 Points

Oh i see now, thanks a lot Andren!