Learning Swift or any other programming language can be exciting and fun, but lessons can only get you so far. To truly master coding, it's important to apply your new skills to exercises and projects that build on all your previous knowledge. That's why we put together these 10 exercises for Swift beginners.
Before you get started, we recommend that you first understand the fundamental concepts of Swift before taking on these challenges. And it never hurts to understand some foundations of coding to make sure you're developing good habits from the start.
10 Swift coding challenges
Below are 10 coding challenges for beginners that'll help you become more familiar with Swift. Half involve strings, and half involve numbers. For each exercise, we've also included a slightly more difficult challenge to take on once you've mastered the basic exercises.
1. Singular or plural?
Create a function that accepts a string as an input. If the string ends in "-s," then the string should return TRUE to indicate that the word is plural. Otherwise, it should return FALSE. Keep in mind that the function should return a Boolean value (TRUE or FALSE) and not a string value ("true" or "false").
We like this challenge because it's easy to step up the difficulty if you're feeling up for it. Your function can include cases that handle irregular plurals — "women," "children," "geese," and so on. You can do this using conditional statements within your function.
2. Reverse a string
Come up with a function that takes an input string and reverses its order. For example, "I love Codecademy!" should return "!ymedacedoC evol I."
For an added challenge, switch capitalization so that each new word is capitalized. So, "I love Codecademy" outputs as "Ymedacedoc Evol I."
An isogram is a word that has no repeated letters. For example, "today" is an isogram, but "tomorrow" is not. Create a function that accepts an input string and returns TRUE if the word is an isogram and FALSE if the word is not.
As an added challenge, if the word is an isogram, output the repeated letter or letters instead of outputting TRUE. So, an input of "tomorrow" would output ["o", "r"].
4. Find the palindrome
You may remember that a palindrome is a word that's spelled the same backward and forwards — like "mom" or "racecar." Create a function that accepts a string as input and returns TRUE if the string is a palindrome.
For an extra challenge, find a way to check whether a phrase or sentence is a palindrome — for example, "Taco Cat" or, "Too bad I hid a boot." You'll have to find a way to remove spaces in your phrases to get the function to work.
5. Name cards
Imagine that you're creating name cards for a group of international guests. Their names and home countries can be stored as key-value pairs in a dictionary:
guests = [ “Mike” : “Canada”, “Jane” : “Australia”, “Angela” : “Germany”, “Robert” : “Nigeria”]
Create a function that accepts a name as an input string and outputs a name card greeting that includes the person's name and country. For example, “I’m [name], and I’m from [country].” So, inputting Robert's name would output, "I'm Robert, and I'm from Nigeria."
If a name isn't in the dictionary, then the output should just include the person's name.
Looking for a further challenge? If a person's name isn't already in the dictionary, display a message like, "New name detected. Please enter country," and prompt the user to enter a country name. The name and country should then get added to the existing dictionary. Hint: use readLine() to accept user input.
6. Factorial challenge
Remember factorials from high school? The factorial "!" operator means that you should multiply a number by all the whole numbers that come before the number. So, 4! is equal to 4 x 3 x 2 x 1, or 24.
Create a function that accepts an integer as input and calculates the factorial of that number. If the number is 4, then the output should be 24.
Looking for more of a challenge? Try validating the user input. If a number is less than zero, then display a message that says, "Input cannot be negative." If the input is 0, then the output should be 1.
7. Ascending order
Create a function that outputs the numbers in a positive numerical input in ascending order. For example, an input of 9462 should output 2469.
To give yourself more of a challenge, add an argument to your function that accepts the strings "ascending" or "descending." Depending on the input of this argument, your function should sort the array in either ascending or descending order.
8. Is there a higher number?
For a given array and number, n, create a function that returns TRUE if a number in the array is greater than n. For example, for an array [24, 32, 42, 34] and n = 50, the output should be FALSE.
For an added challenge, if there are multiple numbers in the array higher than n, return an array of the higher numbers in ascending order. So, for an array [24, 32, 42, 34] and n = 27, the output should be [32, 34, 42].
9. Evens and odds
Create a function that splits an input array into two arrays — one with only even numbers and the other with only odd numbers. For example, an input of [3, 45, 9, 80, 1, 306] will output [[80, 306],[3, 45, 9, 1]].
To make the exercise more challenging, add an additional argument to your function, n. Numbers divisible by n are put in the first output array, and all other numbers go in the second output array. For example, for the input array [3, 5, 18, 10] and n = 3, the output array should be [[3, 18], [5, 10]].
10. Digit length filter
For a given input array and number n, create a function that only outputs numbers with n digits. So, an input of [34, 29, 99, 434, 1, 3] and n = 2 should only output [34, 29, 99].
For an extra challenge, create a function that filters out numbers with a given number of decimal places.
Stand out as a Front-End Developer by mastering Swift
If you're looking to become a Front-End Developer, learning Swift will help you stand out from the pack — especially in app development. And if you're not yet sure what else you should learn, don't worry. Our Career Paths take the guesswork out of figuring out which skills you need to best succeed in your new career as a developer.