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 trial

iOS Swift Collections and Control Flow Control Flow With Conditional Statements FizzBuzz Challenge

Tyler Dotson
PLUS
Tyler Dotson
Courses Plus Student 1,740 Points

i did this and i even watched the solution video and its still wrong someone please help????

saying the fizz logic is wrong and its not returning the correct string.

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
  for n 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"
      } else {
         return "\(n)"
      }
    }

  // End code
  return "\(n)"
}
Ishan Sharma
Ishan Sharma
4,060 Points

You should remove the for loop at line 3.

Code challenge mentions that you don't need to loop inside the function.

Because of the loop, n starts at 1. Since return statement exits function, loop is always returning 1, regardless of value passed to the function.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi, Tyler! First, you're doing great! Your code is actually functional. However, somehow you managed to miss the very last line of the instructions. Take a look:

The challenge also does not need you to loop over a range of values (using for or while). I'll take care of that.

If I remove the for loop (and the open and closed curly braces that go with it), your code passes with flying colors! Try removing that for loop and giving it another "Check work".

Hope this helps! :sparkles:

Tyler Dotson
Tyler Dotson
Courses Plus Student 1,740 Points

Thank you very much i am not much of a person to ready instructions well but now i know. Thank you again.