UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Checkpoint 5 help

Forums > 100 Days of SwiftUI

Hi all! Im really frustratd with this one. I think i understand everything while watching tutorials but then... :) I got something like this:

let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]

let solution = { (_: [Int]) in
    luckyNumbers.filter ( $0 isMultiple(of: 2))
    luckyNumbers.sorted()
    luckyNumbers.map (print("\($0) is a lucky number."))

}

but im only getting errors: Cannot find 'isMultiple' in scope and Generic parameter 'T' could not be inferred

2      

( $0 isMultiple(of: 2))

should be

{ $0.isMultiple(of: 2) }

because isMultiple is a method on Int, not a free function. And because you are passing a closure to filter so you need brackets { }.

That will solve that error, but you still need more work on your solution because as it stands, it won't work.

2      

Hi Poklos

Well done, thats super close!

roosterboy is correct. ive taken the liberty of slightly adjusting your code. im new myself so no expert, but this works.

i dont know why but sorting before filtering works better. dunno why...if someone knows? you need ! in front of $0.isMultipleOf as tou want those that are NOT divisable by 2.

also once youv've stated what thing youre performing methods on, you dont need the nsme everytime within thr same function.

obviously if i've got anything wrong please let me know! just trying to help and be part of the community!

layth

let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]

let solution = { (numbers:[Int]) in
    numbers.sorted()
    .filter {!$0.isMultiple(of:2)}
    .map {print("\($0) is a lucky number.")}

}

solution(luckyNumbers)

2      

Hacking with Swift is sponsored by RevenueCat

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

Learn more here

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.