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

Laksakan Krishnapillai
Laksakan Krishnapillai
6,390 Points

Task help... Explanation, please ?

I have looked at this task on the community but I still can't work out how this is the answer for that task. Can somebody explain how it works to me so I can actually understand it?

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

for n in 1...100 {
    // Enter your code below
    if n % 2 != 0 && n % 7 == 0 {
      results.append(n)
    }
    // End code 
}
Gabriel Morris
seal-mask
.a{fill-rule:evenodd;}techdegree
Gabriel Morris
iOS Development Techdegree Student 789 Points

Breaking it down line by line:

var results: [Int] = []

Create empty array of integers called results

for n in 1...100 {

For every number n between 1 and 100 inclusive

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

If n divided by two has a remainder that does not equal zero AND n divided by 7 has a remainder that does equal 0

      results.append(n)

Append to results array.

So, since you want to check for odd numbers and odd numbers divided by 2 don't have a remainder of 0 you do n % 2 != 0, and since you want numbers that are also multiples of 7 and a multiple of 7 divided by 7 does not have a remainder you complete that line with && n % 7 == 0.

Hope that helps!