Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Collections and Control Flow Control Flow With Conditional Statements Working With Logical Operators

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

Can you help me out on this guys I'm stuck

var results = 20

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

  //End code

}

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

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



    // End code 
}

9 Answers

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

This will make you pass the challenge. I would take look at the videos again or restart some of the older parts of logical operators. Especially for foundational material like this.

var results: [Int] = []

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

the logic I wrote earlier would still apply

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

Hi Shariff for this challenge you have to get a number and append it to the results array if its odd and a multiple of 7. To check to see if a number is divisible you can use the modulo operator (%) and see if the remainder is 0. If the remainder is 0 then the two numbers are perfectly divisible.

let example = 25 % 5 
// 5 divides into 25 perfectly 5 times so there is no remainder that means they're perfectly divisible 

// to check if a number is a multiple of 7 

let randomNumber = 49 % 7 == 0 

You also check to see if a number is odd. For this one its the same idea using the modulo operator( % ) but you will use the logical operator not equal ( != ). This logical operator means not equal to.

let example = 25 % 2 != 0
//if you put this on a playground it will say true on the right hand side because 25 is an odd number. 

Since these are the two parts of the if statement you write you must combine them with the and logical operator ( && ). If both conditions are true the code executes if even one is not true the code doesn't run.

if (49 % 7 == 0)  && (25 % 2 != 0) {
 } 

lastly to append to the results array use dot notation and ( ) to append n to the results array. 

```swift 
let results: [Int] = [ ] 

// we need to put :[Int] since its an empty array and the compiler needs to know the type since this is an empty array

example.append( )

hope this helps !

Thank you but I'm still having challenges on this

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

What part is confusing maybe I can explain it differently

For real this part its a beat difficult to me i have almost a week trying to work on it but its kinda trick

At least now i know where its going but im in a war with that

Cheers l see i was not using .append

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

ah I see yeah at the end they want you to append n to the empty array glad you passed it !

Yeah Jonathan thank you so much lemme try to work until i reach your level

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

happy to help learning to code is just time and practice I don't know everything either