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 trialCameron Tredoux
iOS Development Techdegree Student 750 PointsWould this be an efficient, or even correct, way of making the FizzBuzz Generator?
// I only want to go to 50 :P
for var numbers = 1; numbers < 51; numbers++ {
if numbers % 3 == 0 {
println("Fizz \(numbers)")
} else if numbers % 5 == 0 {
println("Buzz \(numbers)")
} else if numbers % 3 == 0 && numbers % 5 == 0 {
println("FizzBuzz \(numbers)")
} else {
println(numbers)
}
}
2 Answers
Mathieu HAYS
9,623 PointsYour third if will never be reached because one of its condition is already met earlier.
Moath Maharmah
3,613 PointsYay! :P
Cameron Tredoux
iOS Development Techdegree Student 750 PointsCameron Tredoux
iOS Development Techdegree Student 750 PointsOhh so should I put the FizzBuzz as the if and the rest as the else if? SO that it gets checked first?
Mathieu HAYS
9,623 PointsMathieu HAYS
9,623 PointsExactly. You should always put the most specific tests first :)
Cameron Tredoux
iOS Development Techdegree Student 750 PointsCameron Tredoux
iOS Development Techdegree Student 750 PointsYay! Thanks a bunch man ")