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

nader adam
PLUS
nader adam
Courses Plus Student 548 Points

what is the wrong here

func fizzBuzz(n: Int) -> String { // Enter your code between the two comment markers for x in 1...100{ if (x % 3 == 0) && (x % 5 == 0){ print("FizzBuzz") } else if (x % 3 == 0){ print("Fizz") } else if (x % 5 == 0){ print("Buzz") } else { print(x) } } // End code return "(n)" }

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
  for x in 1...100{
  if (x % 3 == 0) && (x % 5 == 0){
  print("FizzBuzz")
  } else if (x % 3 == 0){
  print("Fizz")
  }
  else if (x % 5 == 0){
  print("Buzz")
  }
  else { 
  print(x) 
  }
  }
  // End code
  return "\(n)"
}

2 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

Ok well for starters, I advise you re-read the directions, you are supposed to return the string values, not print them. Also for some reason you are looping through the values 100 times. Remember we are not actually performing the full FizzBuzz challenge where you do the loop, instead you are seeing if the provided parameter value returns either fizz, buzz or fizzbuzz. Finally, your code will always return the number, even if it is divisible by either 3 or 5.

So let's get to work fixing this up:

For starters let's get rid of that loop:

func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
  if (x % 3 == 0) && (x % 5 == 0){
  print("FizzBuzz")
  } else if (x % 3 == 0){
  print("Fizz")
  }
  else if (x % 5 == 0){
  print("Buzz")
  }
  else { 
  print(x) 
  }
  // End code
  return "\(n)"
}

Now let's change those print statements to return statements:

func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
  if (x % 3 == 0) && (x % 5 == 0){
  return "FizzBuzz"
  } else if (x % 3 == 0){
  return "Fizz"
  }
  else if (x % 5 == 0){
  return "Buzz"
  }
  else { 
  return x
  }
  // End code
  return "\(n)"
}

Now for the next part we need to remember what it is we are doing:

The program is going from an integer to a string: n:Int -> String, using the provided variable n, to convert. So with that in mind, lets get replace the x with n, more for staying with the provided parameters than anything else.

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"
  }
  else { 
  }
  // End code
  return "\(n)"
}

Finally let's place our final return statement in the proper location:

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"
  }
  else { 
  // End code
  return "\(n)"
  }
}

And that's it we are done, if you have any questions or concerns please let me know.