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 Basics (retired) Control Flow Exercise: FizzBuzz Generator

How to check a number only?

Could anyone help me with this? I wanted to create something that you can check a number instead of numbers in a range.

var number = 80

for aNumber in number { if (aNumber % 3 == 0) { print("Fizz") } else if (aNumber % 5 == 0) { print("Buzz") } else if (aNumber % 3 == 0) && (aNumber % 5 == 0) { print("FizzBuzz") } else { print("nill") } }

1 Answer

Paul Brazell
Paul Brazell
14,371 Points

Not sure if I entirely understand your question. Do you mean that you want to check just one number for the condition? If that is the case, then you would just not do a for in loop. Just do the if statement on the number.

var aNumber = 80

if (aNumber % 3 == 0) { 
     print("Fizz") 
} 
else if (aNumber % 5 == 0) {
     print("Buzz") 
}
 else if (aNumber % 3 == 0) && (aNumber % 5 == 0) { 
     print("FizzBuzz")
 } 
else {
     print("nill") 
} 

Provide more details if that doesn't answer your question.

It seems like that's what is being asked.

Also Ge Wang, will want to move the code around for logic:

var aNumber = 80
if (aNumber % 3 == 0) && (aNumber % 5 == 0) { 
     print("FizzBuzz")
}
else if (aNumber % 3 == 0) { 
     print("Fizz") 
} 
else if (aNumber % 5 == 0) {
     print("Buzz") 
}
else {
     print("nill") 
}