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

what is wrong

what is mistake?

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

for n in 1...100 {
    print("\(n * 2 - 1)")
    results = [n]
}

results

The question is asking you to append number to results IF they are both odd and multiple of 7.

You need to find a logic to get those numbers by using %. If a number % 2 == 1, that's an odd number. And a number % 7 == 0, that number is multiple of 7. By using && to combine both logics together in an IF statement. You should pass the code.

for n in 1...100 { if n % 2 == 1 && n % 7 == 0 { results.append(n) } }

1 Answer

Mitchell Richmond
Mitchell Richmond
5,401 Points

You need to write an if statement that determines if the number is odd AND a multiple of 7. You can do this by first finding out if the number divided by 2 has a remainder, if it has a remainder it is an odd number. Then you can find out if the number divided by 7 has a remainder, if it does not have a remainder then it is a multiple of 7. Here is how I did it:

var results: [Int] = []

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

  results.append(n)

  }

// End code 

}

So I said, if the remainder is NOT 0 when divided by 2 and if the remainder IS 0 when divided by 7, append the number.