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 trialTyler Hart
7,726 PointsHey, everyone, I am not sure what I am doing wrong here. I can't get anything to show up in the console for the Fizzbuzz
func fizzBuzz(n: Int) -> String { // Enter your code between the two comment markers
for i in 1...100 {
if (i % 3 == 0) && (i % 5 == 0) {
print("FizzBuzz")
} else if (i % 3 == 0) {
print("Fizz")
} else if (i % 5 == 0) {
print("Buzz")
} else {
print(i)
}
}
// End code
return "\(n)"
}
Jonathan Mazala
9,860 Pointsyou are passing n into your function but never using it.
also if you are trying to run this in playground please understand it's extremely buggy and often requires a restart to get interpretation to work
Bruce Röttgers
18,211 PointsHenry Sabio
If you're trying to get something to print to the console, if I'm not mistaken you have to use "console.log()" No this is JS syntax. In Swift it's print()
1 Answer
Bruce Röttgers
18,211 PointsHey,
reading the instructions should clear things out.
Step 1: Enter your code in between the comments shown below. The code is going inside a "function" that will help verify your solution.
Step 2: Change your variable/constant name that you are checking in each step to n. For example if (n % 3 == 0). Note: You don't need to create n, it is already provided.
Step 3: Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz".
Therefore the logic isn't correct. You thought the function should be called and print all numbers out. But in reality the challenge will call your function 100 times and wants to get everytime the number.
Changing your code using the steps provided above:
Original:
for i in 1...100 {
if (i % 3 == 0) && (i % 5 == 0) {
print("FizzBuzz")
} else if (i % 3 == 0) {
print("Fizz")
} else if (i % 5 == 0) {
print("Buzz")
} else {
print(i)
}
}
Changing constant names as declared in Step 2 (and removing for loop as I mentioned):
if (n % 3 == 0) && (n % 5 == 0) {
print("FizzBuzz")
} else if (n % 3 == 0) {
print("Fizz")
} else if (n % 5 == 0) {
print("Buzz")
} else {
print(n)
}
Changing print() to return as declared in Step 3:
if (n % 3 == 0) && (n % 5 == 0) {
return "FizzBuzz"
} else if (n % 3 == 0) {
return "Fizz"
} else if (n % 5 == 0) {
return "Buzz"
} else {
return n
}
Tyler Hart
7,726 PointsThis really helps! Thank you so much !
Henry Sabio
3,680 PointsHenry Sabio
3,680 PointsIf you're trying to get something to print to the console, if I'm not mistaken you have to use "console.log()"