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 trialAlia Khan
3,733 PointsWorking with logical operators challenge
What have I done wrong here?
Thank you
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n % 2 != 0 && n * 7 == 0 {
results.append(n)
}
// End code
}
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Alia! The operator between the n and the 7 is incorrect. That's a multiplication operator. To check if something is equally divisible by a number we use the modulus operator (which you did perfectly when checking for an odd number). Any number % 2 is going to return a 0 or a 1. Five modulus 2 will return a 1 because two goes into five twice with a remainder of one. So all odd numbers will have a one.
To see if a number is evenly divisible by 7 you need to employ the same tactic. 13%7 will return a 6 because 7 goes into 13 once with a remainder of 6. Thus, it isn't evenly divisible. Fourteen modulus 7 will return a 0 as seven goes into fourteen twice with a remainder of 0.
For any number x % y we say that x is evenly divisible by y when the number returned from this operation is 0.
Try again with these hints in mind!