|
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!
|
|
Well I figured out the filter (though I used |
|
If you're not going to be holding on to the transformed array, you should combine these two lines:
into:
That reduces the number of times you have to loop through the entire array. (You are looping once with |
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
I went to the ugly way I guess but the result is here ;)
|
|
@Powergeek I sense a background in C, perhaps even some Basic! What's the saying? If it's stupid and it works, then it's not stupid. (Probably many variations on this.) The only fault I see in your code is that you stopped. Instead, you could have taken the opportunity to refactor and ask that important questions "Can this part be simpler? What about this part??" For example:
Then look inside your loop... I see you sort the array every time you select a new number.
As you refine and refactor, you can chip away at "the ugly" as you called it, and your code becomes more, uh? Swifty! PS: Rather than using bullet points for your code, try the </> tag in the forum message editor. It will format your code for Swift. Excellent! |
|
let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49] let thatEvenNumber = luckyNumbers.filter { $0 % 2 == 0 } print(thatEvenNumber) let ascendingSort = luckyNumbers.sorted { $0 < $1 } print(ascendingSort) let luckySorting = luckyNumbers.map { "($0) is a lucky number." }.joined(separator: "\n") print(luckySorting) |
|
@Obelix How to print each statemnt in a new line? I tried this but didn't work:
|
|
You left off the imporant line!
|
|
|
|
Would this be an acceptable solution?
|
|
Mario asks:
Of course! If you struggled a bit with the logic, and failed once or twice, but ended with a working solution, it's a win! Here and there your code can be tightened. But this will come with experience. For now, don't worry about it, let your logic flow as fast as you can type. First make it work, then revise the code later. One small example:
Here, you are checking to see if some number ($0) is a multiple of 2. The function returns This might be considered redundant, duplicitive, and totally repetitive code. You can simplify it, like so:
Both do the same, which is easier to read?
@twoStraws addresses similar code questions in several of his vids. It often comes down to personal preference, what he calls the Art of Programming. You and your coding team can demolish many pints at the pub, and not answer this question. (See Oxford Comma, Tabs vs Spaces, Duck-Sized Horses, etc.) Keep coding! @twoStraws asks you to code every day to keep your brain working and learning. It's important. |
|
@Obelix Thank you for the breakdown. As a newbie to programming definitely not able to come up with that kind of syntax yet, so it's very very enlightning. |
|
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.