## Checkpoint 5: Lucky Numbers

 Oct '21 For Checkpoint 5 Paul challenges us to filter the odd numbers from an array, sort them, then print each integer in a specific format. Furthermore, he challenged us to do this with closures and one line of code. Try it on your own before peeking at my solution! let luckyNumbers = [ 7, 4, 38, 21, 16, 15, 12, 33, 31, 49] print("\n\n======================= Challenge 5") let _ = luckyNumbers .filter { !\$0.isMultiple(of: 2) } // filter out even numbers .sorted() // sort the odd numbers .map { "\(\$0) is a lucky number." } // map to strings .forEach{ print(\$0) } // print each resulting string 4 Oct '21 Well I figured out the filter (though I used \$0 % 2 != 0 rather than !\$0.isMultiple(of: 2) ) sorted and map but completely forgot about the .forEach() being available. Doh! 4 Oct '21 If you're not going to be holding on to the transformed array, you should combine these two lines: .map { "\(\$0) is a lucky number." } // map to strings .forEach{ print(\$0) } // print each resulting string into: .forEach { print("\(\$0) is a lucky number") } That reduces the number of times you have to loop through the entire array. (You are looping once with filter, once with sorted, once with map and then one final time with forEach.) Not such a big deal with a small number of items, but it can add up. 3
 SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge. Sponsor Hacking with Swift and reach the world's largest Swift community!

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

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