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 trialSoham Ray
884 PointsYour answer is inefficient, why do you need to check for a fizzbuzz, if u just do fizz as a print instead of println...
This code is more efficient I believe tell me if I'm wrong
let inputNumber = 20 if inputNumber%3==0 {print("Fizz")} if inputNumber%5==0 {println("Buzz")}
of course you can change the let in the code for different numbers
1 Answer
Animesh Mishra
2,393 PointsHi, the task is to print:
- "FizzBuzz" if the number is divisible by BOTH 3 and 5,
- "Fizz" if the number is divisible by 3 but NOT 5, and
- "Buzz" if the number is divisible by 5 but NOT 3
Your solution is correct too. You are first checking if the number is divisible by 3, printing "Fizz" if it is, then checking if it is divisible by 5, printing "Buzz" in the same line followed by a newline (through println), hence making it look like "FizzBuzz". It works (although a bit hard to read for me). The only difference between yours and Amit's solution is the input that the program is expecting.
EDIT: Swift 2 has deprecated println() so your solution wouldn't work anymore.
In your algorithm, the program logic is expecting ONE single number to be checked. Amit's program logic is expecting a RANGE of numbers to be checked against the rules and output to be printed accordingly. Hence the difference in implementation. There are no inefficiencies involved. Hope this clears everything up.
erika harvey
Courses Plus Student 14,540 Pointserika harvey
Courses Plus Student 14,540 Pointsi'm not sure I totally understand your question, but in fizz buzz you have to check for fizzbuzz before fizz or buzz because you tell it to stop checking if those two conditions are met andif not then you can continue to check for fizz or buzz. (if you don't do it this way, the computer would either hit three or five and stop not checking all the way for 3 or 5)