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

William Hartvedt Skogstad
William Hartvedt Skogstad
5,857 Points

Challenge Task 1 of 1 For this challenge, we'd like to know in a range of values from 1 to 100, how many numbers ...

Challenge Task 1 of 1

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 you can either use the remainder operator and 3, or use the not operator to check for "not even"


I need help with how to check if a number is a multiple of 7.

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

for n in 1...100 {
    if n == n % 3 = 0 && n == (7,14,21,28,35,42,49,56,63,70) {
        results.append(n)
    }
}

9 Answers

Hello, you need to put your conditions in a formula rather than hard coding them. Please check my solution below.

var results: [Int] = []

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

Hope it helps

William Hartvedt Skogstad
William Hartvedt Skogstad
5,857 Points

Thanks a lot for you help! One more question tho. The code challenge didn't work with the remainder operator using 3 like this:

if (n % 7 == 0 && n % 3 == 0)

Why not?

Ricky Lim
Ricky Lim
2,537 Points

I used "results += [n]" instead of "results.append(n)" but it didn't work.

Is there something wrong with using "results += [n]" for this task?

I understand your code, but I had first worked on a different solution:

var results: [Int] = []

for n in 1...100 {
    if !even && (* 7) {
        results.append(n)
    }
}

Can you or someone else explain to me why my version is faulty, and also how I should know to use the % (modulo) to complete the challenge when it wasn't even mentioned in the preceding video?

These Swift challenges seems unnecessarily difficult to a beginner...

var results: [Int] = []

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

I know I'm missing something, but can anyone tell me why we do we type n % 7 == 0. I understand the n % 7 part, whats the story with the 0? Can someone please break this down for me.

Grayson Hary
Grayson Hary
5,033 Points

Addressing Sapho's question about the 0.

n % 7 == 0

As you know, % is our remainder operator, so we're seeing how many times 7 fits into n, and if that remainder is anything other than 0, we know it is not a multiple of 7.

Anytime. The best way to check if a number is odd or even is to check it remainder dividing it by 2. n%3 == 0 will not work because, for example, 11 is odd but not divisible by 3, so it will return false. Thanks

William Hartvedt Skogstad
William Hartvedt Skogstad
5,857 Points

So they were misleading with the use of the number "3" to check for odd numbers in this challenge?

I think so

You are welcome :)

Rich Braymiller
Rich Braymiller
7,119 Points

i din't get what the remainder does?? Sorry if this is a stupid question....

Shreehari Aravind
Shreehari Aravind
809 Points

If you use the function 7 % 2, the reminder will be 1.

The remainder function gives you the remainder when you divide one number by another.

richard barnes
richard barnes
2,949 Points

var results: [Int] = []

for n in 1...100 {

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

}

just out of interest, is this also a correct way of performing the task. crashed the challenge though?

var results: [Int] = []

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

var ree: [Int] = []

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

These are the same code. Just know they both output the same result if you run it in Swift Playground but for some reason Treehouse compiler seem to show that the second method is incorrect.

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