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

Rodrigo Alarcon
Rodrigo Alarcon
8,028 Points

Swift 3 collections and control flow

Im having trouble understanding how to do this. I understand the videos, but dont really know where to start. please help me.

the challenge is-

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 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"

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

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


    // End code 
}

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

use the modulo (mod) operator (%) like (n % x = remainder). it returns the remainder of dividing the first number (n) by the second (x). so 9 % 4 = 1, 13 % 5 = 3, 18 % 7 = 4, etc. any number mod a larger number is itself, because the larger number goes into the smaller number 0 times, with the smaller number as remainder, so 2 % 7 = 2 etc. if a smaller number divides evenly into a larger number, there is no remainder and therefore the result of the mod operation is 0. so the even-ness of a number can be determined by getting (the number) % 2. if a number mod 2 is 0, 2 divides into the number evenly with no remainder, the number is even. if the result is 1, it is an odd number.

George Paskalev
George Paskalev
3,192 Points

Like James said, you have to make sure the number's mod 2 is NOT equal to 0 and mod 7 is equal to 0.

Here's the code I used to pass the challenge:

for n in 1...100 {

if (n % 2 != 0) && n % 7==0 {

    results.append(n)
}

}