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 trialSean Lafferty
3,029 Pointslittle help please
Could use a wee bit of help, dont really understand the necessary syntax here
func fizzBuzz(bungus: 12) -> String {
// Enter your code between the two comment markers
if (bungus%3<=0){
print("fizz")
}
else if (bungus%5<=0){
print("Buzz")
}
// End code
return "\(bungus)"
}
2 Answers
Mitch Little
11,870 PointsNo problem at all!
The compiler in Xcode will run this. However you should try to follow the specific instruction they give you on the code challenge to convert it to a function.
The overall answer should from this:
for n in 1...100 {
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)
}
}
To This: (As Treehouse asks you to replace all constant values with n, and change all print statements with return statements. It also says to remove an else statement as they have already provided this.)
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
if n % 3 == 0 && n % 5 == 0 {
return("FizzBuzz")
} else if n % 3 == 0 {
return("Fizz")
} else if n % 5 == 0 {
return("Buzz")
}
// End code
return "\(n)"
}
Although I have given you the exact answer now, I would really recommend coming back to this challenge in a day or two and try hard to retreive the solution by yourself until you are comfortable. Take a look at this article I have found it really useful. (Coming from a user not even that far ahead of yourself.) http://www.learningscientists.org/blog/2016/6/23-1 Even though I feel comfortable now with all of this, I too was confused a few weeks ago but even now I go back just to make sure.
Keep going !
Mitch
Mitch Little
11,870 PointsHi Sean,
You have the correct idea using an if / else statement however it appears you have slightly confused the use of the function.
The FizzBuzz challenge solution looks something like this:
for n in 1...100 {
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)
}
}
The for in loop iterates over a range of values between 1 and 10 and then the if statement determines what is printed based on the conditions set.
So it looks like you are almost there however make sure you don't confuse an assignment operator ' = ' with the equal to operator '=='.
You also needed to add a condition to print if the number is both a multiple of 3 and a multiple of 5. The reason this condition is at the top of the if statement is because an if statement has short circuit evaluation, which essentially means the statement stops running, as soon as one condition is met.
This may not make sense the first time around, but perhaps come back to it tomorrow and see if you can do it without looking at any help. Reinforcing and practicing knowledge definitely helped me.
Keep up the good work,
Mitch
Sean Lafferty
3,029 PointsTHANKYOU Mitch,
You are a hero! However I still cannot enter this code into the compiler and pass to the next section!
I undestand how it works and the syntax looks correct!
ANy help would be greatly appreciated!
Sean
Sean Lafferty
3,029 PointsSean Lafferty
3,029 PointsThanks very much!
You're a star!