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

Jericoe States
Jericoe States
3,262 Points

Working with logical operators

var results: [Int] = []

for n in 1...100 {

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

}

/* If the number is both an odd number and a multiple of 7, append the value to the results array provided.

having troubles as to why my code is not working. I also can't remember what --> % means or stands for?*/

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

for n in 1...100 {

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

2 Answers

Hi Jericoe!

First: it's not something you have to do, but I recommend using brackets () so it's easy to find out what's going on in code.

Second: % is - if I remember it correctly - called reminder. It gets the value after diving. For example: if you divide 15 by 3, the reminder is 0, because there's nothing left, as you can get number 3 into number 15 5 times. Same for 16/4, 100/20 etc. However, try 10/3. How many times will number 3 get into 10? 3 times - and 3 times 3 is 9. However, we were diving 10. And what do you have to add to 9 to get number 10? Exactly, number 1 so reminder is 1. A few more examples: 15%7 = 1 (7 gets twice into 15 - 7*2 = 14 and what do you need to get 15? 1) 29%5 = 4 (5 gets 5 times into 25 - 5*5 = 25 and what do you need to get 29? 4) 27%8 = 3 (27-8*3 = 27-24 = 3)

And now about the code: it almost works. First, you have to know what are even numbers (2,4,6,8,...) and odd numbers (3,5,7,...). You are working with odd numbers here but to achieve that, you have to work with even numbers first :) So, how to find out what's even number? What's typical for even numbers? Yes, you can divide them by 2 (=you can divide them with 2 and the value of reminder is 0). Write this and then you just have to make the opposite using the "!" (!=). At this moment, it should be okay :)

Jericoe States
Jericoe States
3,262 Points

//1// var results: [Int] = []

//2// for n in 1...100 {

//3// if n % 1 == 0 && n % 7 == 0 {

//4// results.append(n) } }

2*// This line of codes lets us insert a loop using integers ranging from 1 to 100, easy.

3*// (if n % 1 == 0) if I replace %1 with %2 or %0, wouldn't that make them even numbers,,, integers?

3*// Where exactly would you put the (!=) --> ( if (!)n % (!)1 == 0 && (!)n % (!*)7 == 0)

n%2==0 -> even numbers !(n%2==0) -> odd numbers

You may even add "!" right to equals (n%2!=0), however this caused a problem as Xcode misinterpreted and therefore it didn't work the way it should