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 trialMiguel Correia
1,216 PointsWorking with Logical Operators
I already know the answer of the quiz and it's quite logical but me as a student ho got through all of other quiz's this one is impossible, or you go check the answer or the probabilities you get this one right are minimum. Just wanted to get that in mind.
3 Answers
Chase Marchione
155,055 PointsHi Miguel,
Since the for loop (which is used to cycle through numbers 1 to 100, one by one) is already written for us, we get to write the if statement.
This if statement will logically test for every number covered by the for loop (1 to 100) if the number is both an odd number and a multiple of 7.
An even number never has a remainder when divided by 2. Therefore, we can deduce that if a number has a remainder after being divided by 2, it is an odd number. To evaluate for remainders, we use modulus division, using the % symbol.
If a number is a multiple of 7, then it should divide by 7 evenly, with no remainder.
That covers our if statement. After that, the challenge asks us to append the number to the results array if it passes our if statement. As stated in the for loop, we're referring to each of those numbers from 1 to 100 as 'n'. Thus, if a number passes the if conditions, then n gets added to the results array.
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n % 2 != 0 && n % 7 == 0 {
results.append(n)
}
else {
}
// End code
}
Hope this helps!
Jacob Terrell
2,505 PointsThe code challenges are not made to be help us learn. They frustrate us so we look up the answer elsewhere. Dial down the difficulty for people who are just learning to code.
MIchael Montoya
3,222 Points"An even number never has a remainder when divided by 2. Therefore, we can deduce that if a number has a remainder after being divided by 2, it is an odd number. To evaluate for remainders, we use modulus division, using the % symbol."
What if the number was 0? Would this then show up as odd number since 2 cannot go into it?