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

Apologies but i cant seem to work this one out.

Would it be possible top send me through the answer with a bit of detailed description?

I feel like I'm nearly there but cant crack it

operators.swift
var results: [Int] = []
var index = 0
for n in 1...100 {
    // Enter your code below
    if (n / 7) && (n / 2) {
    results.insert(n, at: index)
    index += 1}
    // End code 
}

1 Answer

First, there is no need to create a new variable named index in order to append to the results array. This is an answer that satisfies the requirements. Often there is more than one way to pass a challenge. If you have any questions about the details of my answer below and would like me to explain it in more detail, please let me know. If not, I encourage you to try and find a different solution to the challenge that still satisfies the requirements to make sure you understand the concepts.

var results: [Int] = []

for n in 1...100 {
    // Enter your code below
    if n%2==1 && n%7==0 {  // check If the number is both an odd number and a multiple of 7(using the % operator)
        results.append(n)
    }
    // End code 
}

Thank you very much for the promt reply. I had forgotten about the results.append function. I will have a play around later and see if i can solve this another way.