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 2.0 Collections and Control Flow Control Flow With Conditional Statements Working with Logical Operators

Aditya Gawade
Aditya Gawade
1,182 Points

need help to solve

i have not entered any code cause I'm blank

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 of 7.

To start us off, I've written a for loop to iterate over the desired range of values and named the local constant n. Your job is to write an if statement inside the for loop to carry out the desired checks.

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

Hint: To check for an odd number use the not operator to check for "not even"

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

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

    // End code 
}

1 Answer

Jari Koopman
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jari Koopman
Python Web Development Techdegree Graduate 29,349 Points

Hi Aditya,

You need to check for 2 things and if they're both true add them to an array. The easiest way of doing this is using an IF statement. To check if a number is a multiple of 7, the division of that number by seven should be 0. To check if that's true you can use the Remainder operator (%). To test if it's an odd number you can check the same because even numbers will always have a remainder of 0 if you divide them by 2. (Use a NOT operator to check for an odd number).

I hope this helped enough. If you are still not able to figure it out leave a comment and I'll help some more!

Regards, Jari

Aditya Gawade
Aditya Gawade
1,182 Points

thank you :) using the % operator is the key i was missing

Aditya Gawade
Aditya Gawade
1,182 Points

i still have trouble writing the code though . var results: [Int] = []

for n in 1...100 { // Enter your code below if n = ((%(n/2)!= 0)) && n = ((%(n/7)=0)){ var results = n } // End code }

this is how i tried to check and how if true< how exactly to add them to results?

Jari Koopman
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jari Koopman
Python Web Development Techdegree Graduate 29,349 Points

A working solution can look like this:

var results: [Int] = []

for n in 1...100 {
    // Enter your code below
   if n % 2 != 0 /*checks for an odd number*/ && n % 7 == 0 /*checks for a division of 7*/ {
  /*and than to add the n to the array use the .append method*/ 
    results.append(n)
    } 
    // End code 
}

Key is to use the % operator just like you would use a + operator, and to add something to an array use the .append method.

Regards, Jari