Lou Franco
Posted on January 20, 2020
In the last article, I gave some exercises for functions. Here are some more to cover the more advanced features.
For these exercises, we're going to use exercises imagined around a diet tracker. In your playground copy this code
let foodCalories = [
"Apple": 72,
"Bagel": 289,
"Banana": 105,
"Carrots": 52,
"Egg": 102,
"Butter": 102,
"Cookie": 150,
"Cola": 136,
"Water": 0,
]
This code:
let food = foodCalories.randomElement()
food?.key
Will tell you a random food from the list. food?.key
is of type String?
because if food
is an empty dictionary, we won't be able to find a randomElement.
This code:
let firstFood = foodCalories.first
firstFood?.key
Will tell you the "first" food from the list, which is also kind of random (but should be the same each call), because dictionaries don't have an order.
Now:
-
Make a function that takes the
foodCalories
dictionary as a parameter and return a random food.func randomFood(foodCalories: [String: Int]) -> String?
Take the first function, copy it, and change it so that you don't have to use a label for the first argument. Change it so that we can call it like
randomFood(foodCalories)
instead ofrandomFood(foodCalories: foodCalories)
Take the first function, copy it and change it so that you use
from
as a label for the first argument. Change it so that we can call it likerandomFood(from: foodCalories)
.-
Make a function that takes the
foodCalories
dictionary as a parameter and returns the "first" food.func firstFood(foodCalories: [String: Int]) -> String?
This one is hard ... Make a function that takes a function of the type that
randomFood
andfirstFood
are, and calls it three times and returns a String array with the results of the three calls.
Try the last one. If you need help, read on ...
The type is ([String: Int]) -> String?
. This means, "a function that takes a dictionary and returns an optional String”. Both randomFood
and firstFood
match this type. You can make a function that takes this type as a parameter.
Try it ...
Ok, here's the function declaration
func threeFoods(foodCalories: [String: Int], foodPicker: ([String: Int]) -> String?) -> [String] {
return []
}
You can call this function like this:
threeFoods(foodCalories: foodCalories, foodPicker: randomFood(foodCalories:))
This says -- give me three foods from the foodCalories
dictionary using the randomFood
function to pick them.
Fill in the threeFoods
function so it does this. Inside that function, foodPicker
is a function you can call. Functions can be passed to functions, just like Strings, Ints, etc.
There are tricky things here regarding optionals and building the array. DM me or comment below if you need help.
Next:
Now that we know the basic types, collection types, control flow, and functions. For the next few chapters I will give exercises that review all of those topics before moving on.
Posted on January 20, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.