Updated for Xcode 14.2
Now that you’re able to use conditions and loops, I’d like you to try a classic computer science problem. It’s not hard to understand, but it might take you a little time to solve depending on your prior experience!
The problem is called fizz buzz, and has been used in job interviews, university entrance tests, and more for as long as I can remember. Your goal is to loop from 1 through 100, and for each number:
So, here are some example values you should have when your code runs:
Before you start: This problem seems extremely simple, but many, many developers struggle to solve it. I’ve seen it happen personally, so don’t stress about it – just trying to solve the problem already teaches you about it.
You already know everything you need to solve that problem, but if you’d like some hints then I’ll add some below.
Please go ahead and try building the playground now.
Hacking with Swift+ subscribers can get a complete video solution for this checkpoint here: Solution to Checkpoint 3. If you don’t already subscribe, you can start a free trial today.
Still here? Okay, here are some hints:
.isMultiple(of:)
. For example, i.isMultiple(of: 3)
.else
block to handle all other numbers.&&
to check for numbers that are multiples of 3 and 5, or have a nested if
statement....
rather than ..<
.SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.