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

Ravirayappan Chinnappan
PLUS
Ravirayappan Chinnappan
Courses Plus Student 11,293 Points

how to find multiplies of 7

how to find the multiplies of 7.

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

for n in 1...100 {
    // Enter your code below
    if n % 3 && n % 7{
   results.append(n)
   }

    // End code 
}

3 Answers

Hi there,

I took a slightly different route to you ... I tested for "oddness" by asking if n % 2 != 0; then check if n % 7 == 0. If both are true, append the result. That looks like:

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

Every odd number isn't divisible by 3. Take 7, for example, 7 % 3 != 0, but it is odd. (I'm trying to figure out what the question means when it mentions using 3)

I hope that helps.

Steve.

Testing for odd could also look like n % 2 == 1, I think.

[Edit] Yes, that also worked fine.

Cool! I'm on this question and I see how you got the n % 2 != 0
I'm wondering how you knew to put your if statement inside the for statement in the parentheses? The student who posted this question didn't have those parentheses, curious how you knew, and what is the rule of thumb for parentheses? I have had issues when knowing when to use the parentheses in the past.

Hi Juan,

Swift is far less picky about usage of parentheses. I'm more of a Java man, hence I added the brackets. It is entirely possible that the brackets aren't required with Swift code! Sorry to have added confusion - that wasn't intended.

Steve.

Paul Je
Paul Je
4,435 Points

What does == mean and what's difference between that and = again? Thank you in advance!!

Hi Paul,

I answered this on another post you asked on - do you understand that answer?

https://teamtreehouse.com/community/dont-know-how-to-get-a-multiple-of-7-for-the-challenge

Steve.

Paul Je
Paul Je
4,435 Points

Yes I do, sorry for asking it twice wow that's my bad. Thank you!

No problem, I was just checking you weren't asking again because the first answer made no sense!

Steve.