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 trialthomas lotocki
3,230 PointsArray intead of range not working
I tried to declare let numbers = [1...20] instrad of doing that right in the for in loop. For some reason it is not working. Is that a wrong type?
3 Answers
Sam Chaudry
25,519 PointsHi thomas, here's a basic example of what you are after, I can't tell you whats wrong without seeing the code, but here's a quick over view of what I think your trying to do.
//1. Create an array of numbers with a range from I through to 10
let numbers = [1...10];
//2. Create for loop and go through array
for number in numbers {
//3. Print number range
print(number);
}
thomas lotocki
3,230 PointsHi Sam,
Sorry for not including the code. So here are two examples I suppose I do not declare the array correctly and that is the reason for code not to work. The example that works for me is:
var numbers = 1...100
for number in numbers{ if ((number % 3 == 0 && number % 5 == 0)){ println("Fizz Buzz") } else if (number % 5 == 0){ println("Buzz") } else if (number % 3 == 0){ println("Fizz") } else { println(number) } }
The one that fails is:
var numbers = [1...100]
for number in numbers{ if ((number % 3 == 0 && number % 5 == 0)){ println("Fizz Buzz") } else if (number % 5 == 0){ println("Buzz") } else if (number % 3 == 0){ println("Fizz") } else { println(number) } }
Can I not declare the array like that?:
var numbers = [1...100]
I'm sorry I did not realise it will not format the code.
Sam Chaudry
25,519 PointsOne of the fundamental principles of Swift is that is does not implicitly convert between types this is why you were getting an error. Stick to the first example you were correct with that.