Leave a rating/review
You’ve learned all about Advanced Combining Operators - so now it’s time for a challenge. For this challenge, you’ll build upon the last challenge, where you constructed some modern day phone numbers. This time, however, the data is a bit more complex.
In the starter project for this challenge, you’ll find an array with a series of 7 digit phone numbers, represented as strings. You’ll also find 2 other arrays: an area code array (that goes before the number) and an extension array that contains the string “EXT” followed by a number. The extension follows the phone number.
To complete this challenge, take the 7 digit number emitted by the publisher and turn it into a phone number of the form AREACODE-XXX-XXXX EXTENSION using what you’ve learned about advanced combining operators.
Here’s a hint: If you end up generating tuples, you can add some formatting by using the map operator and remembering that the first part of the tuple is $0 and the second part is $1 Pause the video, try to come up with a solution, and when you’re ready, resume the video.
Were you able to pair the right area codes, phone numbers and extensions together? Let’s take a look at an example.
example(of: "Making Phone Numbers") {
let phoneNumbersPublisher = ["123-4567", "555-1212", "555-1111", "123-6789"].publisher
let areaCodePublisher = ["410", "757", "800", "540"].publisher
let phoneExtensionPublisher = ["EXT 901", "EXT 523", "EXT 137", "EXT 100"].publisher
areaCodePublisher
.zip(phoneNumbersPublisher)
.map{$0 + "-" + $1}
.zip(phoneExtensionPublisher)
.map{$0 + " " + $1}
.sink(receiveValue: { print($0) })
.store(in: &subscriptions)
}
Did you get this set of numbers to add to your phone book? If so, congrats! In the next episode we’ll wrap up this part of the course, so when you’re ready, head on over to the final episode.