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 trialBrian Patterson
19,588 PointsDon't know how to get a multiple of 7 for the challenge.
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n / 2 != 0 && 7 / 100 == 0 {
results.append(n)
}
// End code
}
6 Answers
Steve Hunter
57,712 PointsHi Brian,
As above, the mod operator gives you an indication that something is divisible by another number. The same approach can be adopted to test for "oddness" or "evenness", but by using the figure 2.
There's another post on that challenge here that covers off the detail for you.
I hope it helps.
Steve.
Michael De La Cruz
10,800 PointsThis makes sense and all but % was not explained in the videos. Why make that the answer to the question? I figured it out myself how to do it but that is only because I have prior experience of coding.
Luke Pettway
16,593 PointsTry using modulus: %
n % 7 == 0
If the number modulus 7 is zero then it is divisible by 7.
Brian Patterson
19,588 PointsThank you Steve and Luke. It all makes sense now! I was close, but did not have the right operator.
Paul Je
4,435 Pointswhy is it == and not =?
Steve Hunter
57,712 PointsHi Paul,
Double-equals is the comparison operator, so it compares the equality of the two things either side of it and returns true or false depending on whether they are equal or not.
Single equals is an assignment operator, which assigns the value to the right of the equals sign into the variable to the left.
Make sense?
Steve.
joshua86
5,013 PointsThis one took me forever.
var results: [Int] = []
for n in 1...100 {
if n % 2 != 0 && n % 7 == 0
{ results.append(n) }
}
Paul Je
4,435 PointsOkay, thank you for that - could you also clarify why the left side of the && doesn't necessarily require a T/F, whereas the right side of the && requires a ==? I tried to equal the n % 7 to 0, but then the answer was wrong. Is this technically possible and right? Or just entirely wrong on my end?
Steve Hunter
57,712 PointsAn && just needs to be able of comparison - do you have an example to illustrate your point?
Take n % 3 && n % 7 for example. If both mod
operators evaluate to the same thing, the && returns a true.
So,
var a = 12 % 6 // 0
var b = 4 % 2 // 0
var equal = a == b // 0 == 0 -> true
var a = 11 % 6 // 5
var b = 4 % 2 // 0
var equal = a == b // 5 == 0 -> false