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

I have no idea what I am supposed to do in this code challenge.

Here is the question: 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 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"

I really need help since I have no I dealing what I am supposed to do.

operators.swift```
var europeanCapitals: [String] = []
var asianCapitals: [String] = []
var otherCapitals: [String] = []

let world = [
  "BEL": "Brussels", 
  "LIE": "Vaduz", 
  "BGR": "Sofia", 
  "USA": "Washington D.C.", 
  "MEX": "Mexico City", 
  "BRA": "Brasilia", 
  "IND": "New Delhi", 
  "VNM": "Hanoi"]

for (key, value) in world {
    switch key {
    case "BEL": europeanCapitals.append(value)
   }
}

1 Answer

Christian Mangeng
Christian Mangeng
15,969 Points

Hi Joshua,

you need to append only the n values (from 1 to 100) to the "results" list that fulfill both conditions:

1) The value is a multiple of 7. You can check that with the remainder operator (%) as:

n % 7 == 0

This is because if n divided by 7 has no remainder, you can divide n by 7.

2) n is odd. You do that by checking if n is even, first. This works the same way as in 1). If it is even, you can divide n by 2 without remainder. So for n to be odd, this check has to be negated:

n % 2 != 0

Now you have to combine the two conditions using && and pack them into an if statement. If both conditions are fulfilled, n should be added to "results", using

results.append(n)

Good luck

Thank you for taking your time to explain this to me! It was very helpful!