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 trialFawad Mohamed
2,100 PointsRegarding Challenge in Swift Collections and Control Flow
In Collection and Control Flow section, I have been given a challenge to perform
which is to find the odd numbers and also multiple by 7 and to append the result in the array.
My way of performing this code is below, I executed in playground and found the result seems correct. But code challenge is not accepting my answer. Rather i copied a code from web which is second set of code. But it was accepted. Please kindly explain what am i missing here?
My code:
var results: [Int] = []
for n in 1...100 { // Enter your code below if n % 7 == 0 { results.append(n) } print(results) // End code }
This code copied from the web and pasted it.
var results1: [Int] = []
for n in 1...100 { // Enter your code below if n % 2 == 1 && n % 7 == 0 { results1.append(n) } // End code }
1 Answer
Abirbhav Goswami
15,450 PointsYou also need to append all the odd numbers that are also multiples of seven in that range to your results array.
In order to do that, we need to use the 'AND' operator (&&) in the if statement. So the code would look something like:
var results: [Int] = []
for n in 1...100 {
if (n % 7 == 0 && n % 2 == 0) {
results.append(n)
}
}
And then you print the results array using:
for index in 0..<results.count{
print(results[index])
}
alexander88
10,824 Pointsalexander88
10,824 PointsI believe this particular question wants only odd answers, so it should be
2 != 0
on the second line of code.