BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

questions about checkpoint 5

Forums > 100 Days of SwiftUI

It took a bit for me to write up a solution for checkpoint 5, however i've got a slight problem.

here's one of my attempts.

let unluckyNumbers = { (evenNumbers: [Int]) -> [Int] in

    for _ in luckyNumbers {
        luckyNumbers.filter { !$0.isMultiple(of: 2) }
        .map { print("\($0) is a lucky number") }

    return evenNumbers


this prints the proper result 10 times, and i'm not sure why. is there way to only print the resulting array once?



First of all why do you need return array of integers? filter, sort, and map already "handling" values by themsevles. You don't need to loop over in this case.

let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]
luckyNumbers.filter { !$0.isMultiple(of: 2) }.sorted().map { print("\($0) is a lucky number") }

you can just put everything in one line


I returned an array because the final step was for me to print the resulting array after mapping it to the above string. What you posted above was my first attempt but I didn't seem right to me.

let unluckyNumbers = luckyNumbers.sorted().filter { !$0.isMultiple(of: 2) }.map{"\($0) is a lucky number"}

The challenge mentioned using closures as part of the solution. If I remove the for loop I do get the proper result printed once. I should've just went with that one liner instead of questioning myself. Maybe I over thought this problem too much. Thank you though.


If you wanted to print array of strings then you can do as follows

let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]
var array = luckyNumbers.filter { !$0.isMultiple(of: 2) }.sorted()
var strArray: [String] = []
for num in array {
    strArray.append("\(num) is a lucky number")

//or if you want oneliner

var stringArray = luckyNumbers.filter { !$0.isMultiple(of: 2) }.sorted().map { String($0) + " is a lucky number" }

as for closures filter, sort and map, they work with closures already so there is no need to create your own

From Apple Documentation -> Declaration func filter(_ isIncluded: (Self.Element) throws -> Bool) rethrows -> [Self.Element] Parameters isIncluded A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element should be included in the returned array.


Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.