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 trialDerek Khanna
603 PointsI have no idea how to do this. I know the remainder operator is %, but how do I reference it?
I don't know how to do this.
var results: [Int] = []
for n in 1...100 {
if n%2
// Enter your code below
// End code
}
1 Answer
Steven Deutsch
21,046 PointsHey Derek Khanna,
In this challenge we are iterating over a range of Integer values, from 1 to 100, using a for in loop. The first time the loop runs, the first value in the range (1) will be assigned to the constant n. We can then use this constant inside of the loops body to check for some conditions.
The conditions we want to check for are:
1) n % 2 != 0
if the remainder of n divided by 2 is not equal to zero, n must be an odd number
2) n % 7 == 0
if the remainder of n divided by 7 is equal to zero, n must be a multiple of 7
We chain these two conditions using the AND operator &&. This ensures that both conditions must succeed for the operation to result in a true value.
If both conditions succeed, then we append the value of n to the results array.
Each time this loop runs, the value of n will be incremented to the next value in the range from 1...100.
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n % 2 != 0 && n % 7 == 0 {
results.append(n)
}
// End code
}
Good Luck