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

Code challenge does not make ANY sense

First of all does anybody know the solution to this and can explain what's wrong

var results: [Int] = []
var even = 0
var odd = 1
for n in 1...100 {
    // Enter your code below
    if n % 2 = 0 || n * 7 {
    print("Even")
    } else { 
        print("Odd")
    // End code 
}

Second of all why do they do this? Why do they confuse us with weird challenges that don't make any sense. Anyway it's not like they would change it. The videos are super outdated yet they stick with them. We pay a stupid amount for this stuff and we can't get an updated version of the courses. Ridiculous.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I believe you're referring to this challenge but please correct me if I'm wrong.

Let's take another look at the instructions:

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"

What it's asking us to do is go through the list of numbers from 1 to 100. Every time we hit a number that's both odd (not evenly divisible by 2) and evenly divisible by 7 we'll put it in our results array. So 7 will go in our array and 21, but 14 will not as it is even.

A number is even if it is evenly divisible by 2. We check for this with the modulus operator %. Which you've done in your code above. But we need it to be odd, not even. We can do the same thing to find if a number is evenly divisible by 7. A number is evenly divisible by 7 if the number modulus 7 is equal to 0.

There is no mention anywhere in the instructions of printing anything. We also do not need variables even and odd. A number is even if it is evenly divisible by 2.

Take a look at my solution:

var results: [Int] = []

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

For further reference the modulus operator was covered in this video at around 2:27.

And the append() method was covered in this video at around 0:39.

Hope this helps! :sparkles:

Thank you once again :)