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 trialbastianstalder
1,525 PointsCan't find the error in my code for this challenge
Ok, I've tested the following code snippet in a playground. I've then printed the results variable to see what I get. And everything seems to be okay. I get an array with the numbers 7, 21, 35, 49, 63, 77, 91 in it.
What's wrong with that?
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n % 2 == 1 && n % 7 == 0 {
results += [n]
}
// End code
}
2 Answers
Steve Hunter
57,712 PointsHi Bastian,
I think the compiler is complaining that you are assigning a single-element array to the results
array as you have surrounded n
in square brackets.
You can just add n
to the array with += or use the append()
method.
My solution looks like:
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if(n % 2 == 1 && n % 7 == 0){
results.append(n) // or += n
}
// End code
}
So my code is pretty much the same as your, just without those square brackets.
Steve.
bastianstalder
1,525 PointsThank you Steve for your hint. With the append method and no square brackets everything worked.