Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift 2.0 Collections and Control Flow Control Flow With Conditional Statements Working with Logical Operators

What's wrong??

var results: [Int] = []

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

Says Bummer: double check your logical conditions to ensure that the value being appended is both odd and multiple and a multiple of 7. When I compile the code above it meets the requirements.

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

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

2 Answers

Anish Walawalkar
Anish Walawalkar
8,534 Points

your if condition is perfectly fine. However you don't need to multiply n by 7 in the for loop.

comment out this line of code and change your if condition to:

for n in 1...100 {
    // Enter your code below
    // var number = (n * 7)
    if (n % 7 == 0) && (n % 2 != 0) {
        results.append(n)
     }   
    // End code 
}
Tobias Helmrich
Tobias Helmrich
31,601 Points

Hey Ben,

you should check if the numbers from 1 to 100 are multiples of 7 and odd and if they are you should append them to the results array. You almost got it right but you're taking every number from 1 to 100 and you're multiplying it by 7 which is not what the challenge wants you to do. So basically you just have to remove this step from your code and just append n and it should work fine.

Like so:

var results: [Int] = []

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

I hope that helps! :)