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

Andres Lopez
PLUS
Andres Lopez
Courses Plus Student 907 Points

A bit confused...

So my understanding of the question is to extract odd numbers over a range of 1...100 I do not remember going over anything like this in the video ?

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

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

    // End code 
}

1 Answer

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

Hi Andres this one is more of a logic problem. You know you have to take out Int values that are odd, and translate that to code. You have to use the logical operators.

if  (n % 2 != 0) {
    print("this means that n is an odd number")
} else {
   print("the number is even")
}

To see if a number is odd you take the number and use the modulo operator % to see if you get a remainder. To check if a number is odd you want to see if there is a remainder after dividing by 2. For the second part of this you have != 0. This is checking to see if the remainder is not 0. The symbol == is used to see if two values are equal and using an exclamation mark means not or the inverse of what comes after.

!true  // this really means false 
!false // this really means true 

1 == 1 // you are checking to see if the value on each side is equal, this would print true 
1 != 1 // you are saying these value on the left is not equal to the value on the right, would print false since 1 is equal to 1

Also when you use the modulo operator in a Xcode playground and you see 0 come up on the side tab it means the numbers are perfectly divisible. That will help you in other challenges when you need to see if a number is divisible by another.