## SOLVED: Feedback on checkpoint 5?

 Oct '22 After my first attempt at checkpoint 5, it looked like I had solved it by just using a sort of procedural approach. ``````let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49] let oddNumbers = luckyNumbers.filter { \$0 % 2 != 0 } let sortedOddNumbers = oddNumbers.sorted() let sortedOddNumbersAndMessage = sortedOddNumbers.map { String(\$0) + " is a lucky number" }.joined(separator: "\n") print(sortedOddNumbersAndMessage)`````` But, that didn't seem to be the lesson and scrolling down to the hints I saw that I'm supposed to use what we just learned. I borrowed a bit of code from one of the lessons since I want to pass three functions through a function here, I think. ``````let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49] var oddNumbers = [Int]() var sortedOddNumbers = [Int]() func doImportantWork(first: () -> Void, second: () -> Void, third: () -> Void) { first() second() third() } doImportantWork { oddNumbers = luckyNumbers.filter { !\$0.isMultiple(of: 2) } } second: { sortedOddNumbers = oddNumbers.sorted() } third: { let sortedOddNumbersMessage = sortedOddNumbers.map { String(\$0) + " is a lucky number" }.joined(separator: "\n") print(sortedOddNumbersMessage) } `````` It works. But, this can't be right. 2 Oct '22 I don't think you were supposed to interpret .first { } and .second { } so literally. I think he wants you to say luckyNumbers.filter { ... your code ... }.sorted().map { ... your code ... } and then print out the result with a for loop. More like your first answer but with only 1 or 2 lets. Reworking your code, I get the following - ``````for row in luckyNumbers.filter({ !\$0.isMultiple(of: 2) }).sorted().map({ "\(\$0) is a lucky number" }) { print(row) }`````` Xcode helps clean up some warnings by putting parenthesis around closures - I don't really have an opinion on which is more readable. 3 Oct '22 I think I'm having a little trouble grasping these ideas the way they're explained since I'm coming from web languages. If I understand this correctly, we're basically dealing with tons of pre-existing methods which we can pass data and other functions. Sort of like callbacks? And, we can even type in brief shorthand logic inside the closure braces if we don't want to make a function for it? 2 Oct '22 Yeah, I think functions are types just like strings and doubles so you can pass them wherever you want. And a closure is just an anonymous function defined inline. Which is helpful when the pre-existing methods don't do exactly what you want but you don't want to make a whole new named function. The Swift syntax definitely is weird to me, though getting better the more hours I spend with it. 3 Oct '22 My take on it was the one below. ``````func transformAndPrintNumbers(_ numbers: [Int]) { let transformedNumbers = numbers .filter { !\$0.isMultiple(of: 2) } .sorted() .map { "\(\$0) is a lucky number" } for transformedNumber in transformedNumbers { print(transformedNumber) } } let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49] transformAndPrintNumbers(luckyNumbers)`````` 3 Oct '22 Thanks guys. That first example and the last one really helped me realize that what I wasn't grasping was the dot notation and the stringing together of the methods. That is so cool how that works. 3

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.

Sponsor Hacking with Swift and reach the world's largest Swift community!

### Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

You are not logged in