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

Aditya Gawade
Aditya Gawade
1,182 Points

not able to solve complex if statement problems

For this challenge, we'd like to know in a range of values from 1 to 100, how many numbers are both odd, and a multiple of 7.

To start us off, I've written a for loop to iterate over the desired range of values and named the local constant n. Your job is to write an if statement inside the for loop to carry out the desired checks.

If the number is indeed both an odd number and a multiple of 7, append the value to the results array provided.

Hint: To check for an odd number use the not operator to check for "not even" first of all, are my ways to check the conditions correct? secondly, how to add them to results

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

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

2 Answers

Your code is slightly more complex than it needs to be, but its a really good try! In your conditions for the if statement, you only need to check if a number in your loop is odd, and that it is divisible by 7. You don't need to assign anything to n. Also, when you're trying to append the values to your array, you have to make sure that it's actually an array that you're appending. Simply trying to add the numbers onto the array isn't going to work. This is how I did it.

var results: [Int] = []

for i in 1...100 {

    if (i % 2 != 0) && (i % 7 == 0) {
        results += [i]
        print(results)
    }
}

Your attempt was really good so don't give up! I hope this helps.

Aditya Gawade
Aditya Gawade
1,182 Points

it says make sure you are appending correct value to results.

Instead of

results += [n]

you'll want to write:

results.append(n)

Sorry about that!

Rich Braymiller
Rich Braymiller
7,119 Points

its like i cant do an objective without looking them up. I'm taking written notes as well as doing it in playground...just seems like the question are always so vague....discouraging....