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 trialCameron D
426 PointsHow do i correctly use the logical operators to check if the number is not even and is a multiple of 7?
I feel like i overcomplicated the code but i don't know another way to answer it.
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n = (%(n/2) = 0) && (n/7 = 1...100) {
results += 1
}
// End code
}
1 Answer
David Papandrew
8,386 PointsYour instinct to use the modulo operator is correct.
Here's how to properly write the two conditions.
The first condition is looking for non-even numbers. You can do this by using modulo and dividing by 2. If n is even result of the operation will be 0. Since you don't want an even number, we opt for an expression that says "if n % 2 is NOT equal to 0".
For the second condition you want to see if n is a multiple of 7. Again the modulo operator is necessary. Here we will look for n % 7 to be equal to 0.
Assuming n meets these two conditions, we can then use the append method to add the "n" value we are evaluating to our array: results.append(n)
Here is the code:
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n % 2 != 0 && n % 7 == 0 {
results.append(n)
}
// End code
}