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

Abhinav Aggarwal
Abhinav Aggarwal
3,103 Points

not able to do it

my code: var results: [Int] = []

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

// End code
logicalOperators.swift
var results: [Int] = []

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


    // End code 

2 Answers

Hi Abhinav,

You are along the right track but not quite there. It may help to break the challenge into smaller sections.

Challenge 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.

Steps

  1. loop to iterate over the desired range of values
  2. check if n is an odd number
  3. check if n is a multiple of 7
  4. append the value to the results array provided.

Step 1 is done for you.

Step 2 has the hint: To check for an odd number use the not operator to check for "not even"

We can do this by using the modulo method to show the remainder, if a number has no remainder when divided by 0 then this is an even number, we can detect an odd number by negating this check. You have the operation right with:

n % 2

However that will give an Int value and you want a boolean for the condition you require:

n % 2 != 0

Step 3, to check if the value of n is a multiple of 7 then you use the modulo function, not the multiplier and again we want a bool to compare:

n % 7 == 0

Step 4 you nailed.

So if we put it all together we get:

var results: [Int] = []

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

Hope this helps you in your quest.

KB :octopus:

Ben Shockley
Ben Shockley
6,094 Points

I think your math operations are a little off there. You don't have complete equations in your if statement.

Saying if n * 7 doesn't do anything because that's not a true or false statement. saying n * 7 == 14 on the other hand is a true or false statement

You're on the right track, you just need to check the syntax of your if statement, and rethink your math a little bit.

For a hint to check for a multiple, you would want to divide n by 7.