7 Days Swift Challenge - Day 3: Loop in Swift - How to Make an App

in #swift8 years ago

Today we will be talking about one of the most popular feature in programming - Loop.

Let's dive right in!

Your Code Challenge:

This code challenge is a computer science's classic Algorithm drum roll please 🥁🥁🥁.

  • Create a new playground for this code challenge called 7DSC-Day3-MinMaxFinder

  • In Swift, you can create a random number by using arc4random()

  • And if you want to have the random number is within the range of 0 to 1000, you can do arc4random() % 1000.

  • Why do we use % modulus operator here? Modulus means you find the remainder of a division. If you 10%3, you get 1.
    In other words, the remainder of a division is always from 0 to the divisor - 1.

  • Knowing this trick, we can now create any random number in any range we want.

  • Let's create an empty array of Int named numbers.

  • Use a for loop so you can add 1000 random numbers that are always less than 1,000,000 to the array.

  • Now, can you find me the minimum number in the array of random numbers. RULE: do not use any other Swift function like min, max to find the number. You have to use a for loop here! And do not use any other functions to sort the array. You have to use for loop! Otherwise there is no point of practicing loop :( This is in red so you know that I'm seriously serious here! 😈

  • Next, can you help me find the maximum number from the array?
    Finally, share you Code Challenge solution in the comment section with me below!

  • This is the most fundamental algorithm (a proven system to perform a task) in computer science. I hope you will try to figure this out! Please spend at least 60 minutes on this before you move on and watch the solution video.

Have fun and good luck,

  • Duc

Love even more advanced and in-depth training with me? Join Total iOS Blueprint 2 where I share with you how to build 11 iOS apps from scratch with awesome video tutorials just like these.

Get this swift course: https://www.ductran.co/
For more videos: https://www.youtube.com/channel/UCvPFGq6luCqAVGiFpzTvkIA/videos