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 trialJustus Aberson
1,035 PointsI dont understand what to do.
Hey guys, can someone please explain to me what to do? Thanks.
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if
// End code
}
2 Answers
Henrik Brüntrup
5,485 PointsHi Justus,
you need to use the && operator in the if statement and append the numbers that are both unequal and divisible by 7:
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if (n % 2 == 1) && (n % 7 == 0) {
results.append(n)
}
// End code
}
Btw. it is good practice to mark an answer as 'best answer' in your threads, if a successful solution has been posted. It lets others know that the answer has been given and no longer needs to be looked at. I posted an answer to your earlier thread about while-loops and it never received feedback from you.
Henrik Brüntrup
5,485 PointsSure.
Quoting Apple: The remainder operator (a % b) works out how many multiples of b will fit inside a and returns the value that is left over (known as the remainder).
So the line
if (n % 2 == 1) && (n % 7 == 0) {
means, that the remainder operator will work out how many 2s will fit inside 'n' and if the remainder is 1 and (&&) will then work out how many 7s will fit inside 'n' and if there is no remainder, if this is the case it will then
results.append(n)
append 'n' to the 'results' array.
For example 'n' = 14 will not be added to the array because 2 x 7 = 14 -> no remainder - 'n' = 21 will be added because 2 x 10 = 20 (remainder of 1 to 21) or 2 x 11 = 22 (remainder of 1 to 21) and 7 x 3 = 21 ( no remainder)
Justus Aberson
1,035 PointsJustus Aberson
1,035 PointsThanks, Henrik, but I dont understand your code, can you please explain?