DEV Community

Cover image for Simple Swift Challenges (2)
Shanice
Shanice

Posted on

Simple Swift Challenges (2)

Greetings ๐Ÿ‘‹,

I've constructed 3 Swift challenges and listed them below. First, you'll find the challenges and further down the page you'll find the solutions. If you aren't able to solve them, before looking at the solutions, try to find some help through a Google search or at https://developer.apple.com/documentation/swift

  • I struggled a bit coming up with creative ways to write the challenges ๐Ÿ˜…. Please bear with me :)

Have fun :)

  1. Help! I have a list of people and I need to quickly find a certain person. This person's name starts with "Is" and ends with "h". What two methods can you use check a string for these values?

    let personOne = "Isaac"
    let personTwo = "Isiah"

  2. Create a variable for an integer with the value of one million and make it reader friendly.

  3. I have 12 pieces of candy and I have 3 students. Will I be able to split the candy evenly? (Hint: Use a method to see if the 12 is a mulitiple of 3)

    let candy = 12;

*
*
*
*
*
*
*
*
*
*
*
*

  1. Help! I have a list of people and I need to quickly find a certain person. This person's name starts with "Is" and ends with "h". What two methods can you use check a string for these values?

    .hasPrefix("Is")
    .hasSuffix("h")

    for instance:
    let personOne = "Isaac"
    let personTwo = "Isiah"

    personOne.hasPrefix("Is")
    personOne.hasSuffix("h")
    personTwo.hasPrefix("Is")
    personTwo.hasSuffix("h")
    

    these methods will return a Boolean, either true or false, based on whether the string starts/ends with the specified string or not.

    1. Create a variable for an integer with the value of one million and make it reader friendly.

    var million = 1_000_000

    In Swift, you can insert an underscore in the place of a comma to help with readability

  2. I have 12 pieces of candy and I have 3 students. Will I be able to split the candy evenly? (Hint: Use a method to see if the 12 is a mulitiple of 3)

    let candy = 12;

    candy.isMultiple(of: 3)

    can also be written as: 12.isMultiple(of:3)

    This method returns a Boolean value-true if it is a multiple,false if it is not a multiple

Top comments (0)