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

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,602 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! :)