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 trialval
585 PointsWhy do we have to use " for kid in kids " and why cant we just write " let kids = 1...10 if kid % 3 == 0
My code: let kids = 1...10
for kid in kids { if kid % 3 == 0 && kid % 5 == 0 {println("FizzBuzz")} else if kid % 3 == 0 {println("Fizz")} else if kid % 5 == 0 {println("Buzz")} else {println(kid)} }
This is correct, but hy can't I leave out the "for kid in kids" part? like this:
let kids = 1...10
if kid % 3 == 0 && kid % 5 == 0 {println("FizzBuzz")}
else if kid % 3 == 0 {println("Fizz")}
else if kid % 5 == 0 {println("Buzz")}
else {println(kid)}
I know it's not correct but I can't explain why, please help.
2 Answers
Dan Chambers
6,057 PointsIf I understand correctly, the whole point of the 'for-in' loop over the regular 'for' loop is to save on code when you need to iterate through a range or array, because this is such a common task. I think there's a lot happening under the hood that makes it difficult to grasp. I think the example confuses things by using the word 'kid'. I prefer to use 'i' to represent the index like this:
let kids = 1...10
for i in kids { //rest of solution }
The reason the above works is because the declaration, condition, and increment are all implied when you use the 'For-In' loop. I understand it to essentially be shorthand for the below:
let kids = 1...10
for var i = 1; i < kids.count; ++i { //rest of solution }
Hope that explains whats actually happening. I find looking to the Apple documentation straight after a video very useful to iron these things out.
Sameer Vohra
754 PointsTook me a bit of thinking to make the same change Dan suggested, but it is a massive improvement, especially for new programmers. Was going to email Treehouse, but figured they won't re make the video and I saw Dan's post here. Hopefully more people see this if they get stuck on the logic.
Luke Evans
Courses Plus Student 526 PointsLuke Evans
Courses Plus Student 526 PointsI think it's because you haven't defined "kid" anywhere, so the computer has no idea what "kid" actually is. If you changed the constant at the top to "let kid = 1...10" (rather than "let kids= 1...10"), it still wouldn't work: you have defined it as a range of numbers, not an individual number that could be divided by 3 or 5.
The for-in loop defines this "kid" value for you. It takes the individual numbers in the "kids" range of numbers and then, in the { }, it does all the things you've asked it to do (i.e. check the remainders of these individual numbers when divided by 3, etc.)
Hope this helps!