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 trialFahiye Abdi
2,133 PointsWorking with Logical Operators
Could someone explain this challenge?
var results: [Int] = []
var even = 0
var odd = 1
for n in 1...100 {
// Enter your code below
if 100 % 1 = 0 {
print("Even")
} else {
print("Odd")
// End code
}
1 Answer
Cena Mayo
55,236 PointsHi Fahiye,
I think you'll actually want to use
n % 2 = 0
The reason is that the modulo operator is being used here to determine if any given number is even, which means it's divisible by 2. (hence, n % 2 = 0). Since all numbers are divisible by 1, 100 % 1 (or n % 1) will always result in true statement - your loop will never reach the else clause.
Hope that helps! Cena