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 trialKeegan Olson
844 PointsI can get the code to work in x code but the challenge space will not accept my code?
The code seems to work when I don't use the function code in my x code - I can write the whole if else statement and return the values. When I use the function code in my x code, it doesn't have any issues but it returns no values. In the code challenge however it tells me the logic is incorrect but I can't see what it's reading as incorrect logic.
Please help! :)
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) {
return "FizzBuzz"
} else if (i % 3 == 0) && (i % 5 != 0) {
return "Fizz"
} else if (i % 3 != 0) && (i % 5 == 0) {
return "Buzz"
} else {
return "\(i)"
}
}
// End code
return "\(n)"
}
Bartłomiej Kredek
5,421 PointsChange i to n in if statement
1 Answer
Jonathan Ruiz
2,998 PointsHi Keegan, there are two things that are keeping you from passing the challenge. The first is you are using the variable i instead of n. The parameter n in the function needs to be in the if statements you use. Think of it as the name of the value the user will put in, this function will accept an int from the user and you need to use the name n inside the function to call "FizzBuzz" and so on.
if(n % 3 == 0) {
return "Fizz"
// this checks to see if the int value the user inputs is divisible by 3 and will return the string "Fizz" if it does
}
The second reason why it won't accept you answer is in your subsequent if statements. After the first if statement that returns "FizzBuzz" you must check to see if a n is divisible by 3 then do a separate check to see if its dividable by 5. In Xcode it doesn't complain cause the if statements are written as correct code, however the code challenge asks for you something different. This will let the code pass.
func fizzBuzz(n: Int) -> String {
for i in 1...100 {
if (n % 3 == 0) && (n % 5 == 0) {
return "FizzBuzz"
} else if (n % 3 == 0) {
return "Fizz"
} else if (n % 5 == 0) {
return "Buzz"
}
}
return "\(n)"
}
Hope this helps
Jeron Artest
Courses Plus Student 1,587 PointsStill should get rid of for loop.
Jeron Artest
Courses Plus Student 1,587 PointsJeron Artest
Courses Plus Student 1,587 PointsI think whats happening is you don't have to use the for loop. In the challenge I remember it telling me to replace the values in the if statements for the n.
In other words, just keep your if statements.
Take one more look at the directions :)