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

makes me want to quit.

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){ print ("Fizzbuzz") } else if (n % 3 == 0){ print ("Fizz") } else (n % 5 == 0) { print("Buzz") } else { print(i) } // End code return "(n)" }

literally pulled this damn code from the solution video the exact same and it failed for the same errors that i encountered on my own approach. treehouse deeply lacks in the "practice area" we should be taught step by step how to solve these quizzes/challenges then once we have a firm idea on how to solve them then test us. like you know normal education. sorry I'm so pissed but I'm sick of wasting time on these when they feel like the "test is nothing that we were taught" syndrome. or just too far ahead from what we learned

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){ 
  print ("Fizzbuzz") 
  } else if (n % 3 == 0){ 
  print ("Fizz") 
  } else (n % 5 == 0) { 
  print("Buzz") 
  } else { 
  print(i) 
  }
  // End code
  return "\(n)"
}

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! Your logic is solid and there's only one syntax error. The syntax error is here:

else (n % 5 == 0)

An else statement doesn't take a conditional. It's what happens after everything else has failed. You meant to write :

else if (n % 5 == 0)

... just like the line before it.

The primary problem comes with following the instructions of the challenge. They are picky. They even note that in the challenge instructions and give the reason why they are so picky for this particular challenge.

While these are very specific directions, they allow me to verify your solution precisely over a large number of possible answers.

You started off great and made sure that your variable was named n per the challenge requirements. But there were other instructions that you missed.

Step 3: Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz".

You are printing the results and this challenge needs you to return the results.

Also, your code resides inside a for loop, but the challenge instructions say this:

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

This means that the for loop should be removed. So if I edit your code just slightly, it passes the challenge because, as stated, your logic is solid.

Hope this helps! :sparkles: