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

mark hengstebeck
mark hengstebeck
1,119 Points

How to begin with this challenge? Working with logical operators: Objective 1 of 1.

Also, how to write a multiple of 7 in code? var results: [Int] = []

for n in 1...100 { // Enter your code below

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

for n in 1...100 {
    // Enter your code below

    // End code 
}

4 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi mark! Sure, I can provide some hints. When we're checking to see if something is evenly divisible by something else, we use the modulo operator. Take this example: 15 % 3. Three divides into 15 five times with 0 remainder. If we were to do 17 % 3 the number returned would be 2. Three divides into 17 five times with 2 as a remainder. So to check if a is evenly divisible by b we can have this comparison: a % b == 0. If that is true, then a is evenly divisible by b.

The second part requires that we use the append method on the results array. Even numbers are, of course, evenly divisible by two and we're looking for odd numbers. So, if the number passed in is not evenly divisible by 2 and it is evenly divisible by 7 we should append it to the results array. The ending results array should look like this [7, 21, 35, 49, 63, 77, 91]

Here's a bit of pseudo code:

for every number in the range 1 to 100
   if that number is *not* evenly divisible by two AND that number is evenly divisible by 7
       append it to results
   end if
end for

Hope these hints help! :sparkles:

mark hengstebeck
mark hengstebeck
1,119 Points

Hi Jennifer, Thanks for your comment to Michael's. In the future, I will specify that I am asking for a hint in the right direction. These hints will empower me to learn coding and remember that which I have learned. After glancing at Michael's answer, I looked away. Will you provide me with a hint as to my original question? Thanks, Mark

mark hengstebeck
mark hengstebeck
1,119 Points

this is what I want to write: var results: [Int] = []

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

mark hengstebeck
mark hengstebeck
1,119 Points

Hi Michael, That's it! Thanks, Mark