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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.