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 trialIsaac Gonzalez
1,857 PointsAm I correct?
I made this code:
var number = 90
if number % 3 == 0{
println("Fizz")
}
if number % 5 == 0{
println("Buzz")
}
if number % 5 == 0 && number % 3 == 0{
println("FizzBuzz")
}
but on the video shows a "1...10" value so I don't know if I read well the instructions.
2 Answers
Bradley Anderson
Full Stack JavaScript Techdegree Student 9,030 PointsWhen you run this you will get:
Fizz Buzz FizzBuzz
each one of the if statements are true, so the body of them will run. The exercise only wanted one of the three to print out. The expected output is "FizzBuzz".
You need to use else if statements to make sure only one of the conditions will work.
Nate Bird
7,999 PointsYou also want to adjust the order of the if else
statements. You’ll need to place the ‘FizzBuzz’ statement first or it will never be reached because the ‘Fizz’ or ‘Buzz’ will be true before it reaches the last condition.
Here is my take on your approach.
var number = 10
if number % 5 == 0 && number % 3 == 0 {
println("FizzBuzz")
} else if number % 5 == 0 {
println("Buzz")
} else if number % 3 == 0 {
println("Fizz")
}
Frances Angulo
5,311 PointsSo I guess this is not exhaustive - I implemented an else statement. The instructions for the quiz did not say "for a given range, bring a list of fizz/buzz/fizzbuzz" so I think the solution stands!
var number = 25
if number % 3 == 0 && number % 5 == 0 {
println("fizzbuzz")
} else if number % 5 == 0 {
println("buzz")
} else if number % 3 == 0 {
println("fizz")
}
else {
println("else")
}
Isaac Gonzalez
1,857 PointsIsaac Gonzalez
1,857 PointsThank you ^^