There’s lots of new stuff packed into one day this time around as we learn about functions as parameters, closures and manipulating array items.
Paul starts off Day 9 with a warning that we’re about to learn closures and closures are hard. I’ve not come across this kind of thing before so it does look a bit confusing at first, but after working through all the lessons I can see the point and am starting to understand the syntax…kinda.
We also cover the concept of passing functions into functions as parameters, which is slightly mind-bending. And we learn about a few useful features of arrays such as sorted
, filter
and map
.
Checkpoint 5
We’re putting all these new things to practice in Checkpoint 5. Paul gives us an array and our job is to manipulate it such that we…
- Ditch the even numbers
- Sort the array ascending
- Change each item to be a string
…and then print out the resulting new array.
My solution looks like this.
// Input
let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]
// Transform the array
let newNumbers = luckyNumbers
.filter { !$0.isMultiple(of: 2) }
.sorted()
.map { "\($0) is a lucky number" }
// Print out the new array
for i in 0...newNumbers.count - 1 {
print(newNumbers[i])
}