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 trialRavirayappan Chinnappan
Courses Plus Student 11,293 Pointshow to find multiplies of 7
how to find the multiplies of 7.
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n % 3 && n % 7{
results.append(n)
}
// End code
}
3 Answers
Steve Hunter
57,712 PointsHi there,
I took a slightly different route to you ... I tested for "oddness" by asking if n % 2 != 0
; then check if n % 7 == 0
. If both are true, append the result. That looks like:
for n in 1...100 {
// Enter your code below
if( n % 2 != 0 && n % 7 == 0){
results.append(n);
}
// End code
}
Every odd number isn't divisible by 3. Take 7, for example, 7 % 3 != 0, but it is odd. (I'm trying to figure out what the question means when it mentions using 3)
I hope that helps.
Steve.
Paul Je
4,435 PointsWhat does == mean and what's difference between that and = again? Thank you in advance!!
Steve Hunter
57,712 PointsHi Paul,
I answered this on another post you asked on - do you understand that answer?
https://teamtreehouse.com/community/dont-know-how-to-get-a-multiple-of-7-for-the-challenge
Steve.
Paul Je
4,435 PointsYes I do, sorry for asking it twice wow that's my bad. Thank you!
Steve Hunter
57,712 PointsNo problem, I was just checking you weren't asking again because the first answer made no sense!
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsTesting for odd could also look like
n % 2 == 1
, I think.[Edit] Yes, that also worked fine.
Juan Juarez Baldizon
Courses Plus Student 3,352 PointsJuan Juarez Baldizon
Courses Plus Student 3,352 PointsCool! I'm on this question and I see how you got the n % 2 != 0
I'm wondering how you knew to put your if statement inside the for statement in the parentheses? The student who posted this question didn't have those parentheses, curious how you knew, and what is the rule of thumb for parentheses? I have had issues when knowing when to use the parentheses in the past.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi Juan,
Swift is far less picky about usage of parentheses. I'm more of a Java man, hence I added the brackets. It is entirely possible that the brackets aren't required with Swift code! Sorry to have added confusion - that wasn't intended.
Steve.