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 trialCharles Roennfeldt
1,163 PointsHow do I send my 'If' statement results to the 'results' variable?
I believe I have some if not most of the challenge close to completion but I can't seem to work out how to send the results from each loop back to the variable called results. Please help!
2 Answers
Marina Alenskaja
9,320 PointsHi again
So, I used the append method instead:
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n % 2 == 1 && n % 7 == 0 {
results.append(n)
}
// End code
}
Also, you should always write if n then ... (in the code above you are starting with 2%n) If that makes any sense :-)
Marina Alenskaja
9,320 PointsUse the += operator to add the number to the array if all conditions are true: results += n
Charles Roennfeldt
1,163 PointsI tried but it keeps coming up with an error asying "Binary operator += cannot be applied to operands of type [Int] and Int"
Marina Alenskaja
9,320 PointsCan you please copy and paste your code in? Then I can see what's missing :-)
Charles Roennfeldt
1,163 PointsSorry heres the code I have put in
var results: [Int] = []
for n in 1...100 {
if (2%n != 1) && (7/n != 0) {
results += n
}
}
Charles Roennfeldt
1,163 PointsCharles Roennfeldt
1,163 PointsOhhhh I see what you mean. You've cleared up a lot of confusion, thanks for the help!