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

duke Beardslee
duke Beardslee
3,361 Points

what am i doing wrong

what should i change

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

for n in 1...100 {
    // Enter your code below
    if n % 2 = 1 && n % 7 = 0 {
    results.append(n)
    }
    // End code 
}
Nick Frozz
Nick Frozz
31,289 Points

Change "=" on "==". (for both)

3 Answers

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Hi Duke Beardslee

Really close mate ... just need to change the assignment equals ( = ) to a compison equals ( == )

n % 7 = 0  // incorrect
n % 7 == 0 // correct

Nice work otherwise.
Happy coding,

Dave.

Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

Duke Beardslee — You can mark the question solved by choosing a "Best Answer".
And happy coding!

Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

Seriously?
Someone down voted a correct answer?

I'm new to the site and as I look through the forums I see a lot of questions being asked but very few people actually answering. So while the 'community' seems to be more than happy to ask away not a lot apparently are into giving back ... which is fine I suppose, but when you have a member who clearly is happy to give back, downvoting their responses only discourages them from being an active participant in responding (and do keep in mind, I'm paying for the 'privilege' too).

Down voting an incorrect answer is one thing, why you'd downvote a correct one is beyond my understanding?

:x: Strike One :x:

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Hi Dave Harker

First, I would like to just clarify that my original answer was being written as yours was being posted. I only saw yours after mine posted, so I just left both as is (as is common in the Community).

Next, as a fellow student (who is a Moderator as well), I wish I had a more clear answer for you. I honestly have no idea why your response was downvoted?!? It doesn't make any sense to me. Your response is a correct answer. It explained the error specifically and clearly. It was polite.
These are the three main things that are encouraged in the Treehouse Community.

I would like to apologize for the negative experience and let you know that it really isn't common for behavior like that to be present in the Community Forums.

Myself and the other Treehouse Moderators are here and try to make sure the Code of Conduct is adhered to by all students and all times, and that everyone is treated with respect and courtesy. With that said, I have negated the downvote on your answer and have forwarded the link for this thread to the Treehouse Staff for their files.

Please don't let this affect your participation in the Community, as I (and most others) do appreciate your contributions.

Again, there is nothing wrong with your reply, quite the contrary, actually! :)

Thank-you again for helping in the Community! :thumbsup:

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Duke,

You're on the right track, but there are a couple of issues.

First, the single equal sign (=) is an assignment operator, so it cannot be used for comparing values. This is done with the comparison operator which is a double equal sign (==). Once you fix that up, the challenge will pass.

Note: The challenge does give a hint for the "best practice" way to check for an odd number:

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

Checking if modulo 2 equals one works, but really isn't the best way. Using the not operator makes the code more self-commenting, as it is much easier to see what the code is doing. If you scan quickly, seeing n % 2 == 1 doesn't tell someone quickly what it's doing. It's hard to read. However, n % 2 != 0 is a very quick read to see you're checking for odd numbers, without having to pause and think for a second.

While both ways will satisfy the challenge, I just wanted to point out the Best Practice for you.

Keep Coding! :) :dizzy:

Rob Allessi
Rob Allessi
8,600 Points

Unfortunately, it would seem someone is trolling with the downvotes. Upvoted you both for the wonderful assistance and answers. :)