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 trialOliver Alberry
1,690 PointsFizzBuzz Challenge compiler error...works in Xcode.
The error returned is below:
swift_lint.swift:28:1: error: missing return in a function expected to return 'String' } ^
I've tried a little bit of everything to get this to work, but it won't. In Xcode it works fine, but I also had print statements instead of return. Going along with the Treehouse challenge instructions is where I'm running into trouble. It says to leave the return statement that they provide, but I need to include a default statement in order for the switch to work.
Is there something else I'm missing here? I see a lot of people using if/else statements, but I should be able to use a switch, right?
Thank you!! If I had hair, I'd be pulling it out right now!
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
for n in 1...100 {
switch true {
case n % 3 == 0 && n % 5 == 0: return "FizzBuzz"
case n % 3 == 0: return "Fizz"
case n % 5 == 0: return "Buzz"
default: return "\(n)"
}
}
}
1 Answer
kjvswift93
13,515 PointsInstead of using a complicated switch statement, I just used an if - else statement. So if you were to call the fizzBuzz function and pass in a value of 12 for n, then it will print "Fizz".
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
if (n % 3 == 0 && n % 5 != 0) {
return("Fizz")
} else if (n % 5 == 0 && n % 3 != 0) {
return("Buzz")
} else if (n % 3 == 0 && n % 5 == 0) {
return("FizzBuzz")
}
// End code
return "\(n)"
}
fizzBuzz(n: 12)
(!true || !false) && (true && !false)
Oliver Alberry
1,690 PointsThanks! I got it to work. Turns out I was confused by some of the direction- I was assigning a range of values, when treehouse was already taking care of that. Thanks for sharing your example and insight! For some reason I find switch/case statements easier to read than if/else statements, which is why I gave it a shot. I'm going to practice both a bit more.
kjvswift93
13,515 PointsGood work! That's funny because always preferred if/else statements over switch statements, which I had more difficulty understanding when I first began. Keep up the good work.
Oliver Alberry
1,690 PointsOliver Alberry
1,690 PointsI just realized my indentation is a little odd, so I've since fixed it to read better, but it returns the same error.