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

Please i dont understand what question recquires me to do , someone care to explain

I have reviewed some answers but dont seem to know how they got there

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

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

    // End code 
}

2 Answers

Kyle Johnson
Kyle Johnson
33,528 Points

The Code Challenge is asking the following:

Your job is to write an if statement inside the for loop to carry out the desired checks. If the number(n) is both an odd number and a multiple of 7, append the value to the results array provided.

To check is a number is even, you use n % 2 == 0 (check if the remainder of the number is equal to 0). We want to know if the current number is odd so we can check if the remainder of the number is NOT equal to 0 by using n % != 0.

To check if a number is a multiple of 7 we can use the % operator again. n % 7 == 0. Basically asking if the current number divided by 7 has no (0) remainder, then execute code.

The last step is to append the numbers that are both odd and a multiple of 7 to the results array. This can be done with by results.appen(n).

The completed code is 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 
}
Chris O'Brien
Chris O'Brien
6,544 Points

It is looking for you to put an IF statement to find out/sort numbers.

So it has the for loop 1...100 which is checking the range of numbers.

You now need to add the statement the the for loop to work out what its asking i.e append all the numbers that are odd and a multiple of 7. You are going to have to use &&, ==, != in the statements.