|
I solved Checkpoint 5, but how can I solve by making one fuction with this all inside of it? I think I heard Paul say try not to use a bunch of temporary variables, right?
|
|
You are curious and asking the right questions! This is a huge step towards mastering SwiftUI. You don't want us to give you the answer, but we can lead you in the right direction. The Swift language is heavily typed. One of the early skills you develop should be to identify or ask XCode to tell you what type a variable is. For example:
What type is this? You and I can easily see it's a string. But if you hover your mouse over firstName while holding down the option key, your cursor turns into a question mark. Click the variable name. Swift will then confirm firstName is of type String. Get used to doing this with all your variables. Do it a lot! Next get comfortable with the idea that Swift types (Integers, Doubles, Arrays, etc.) have methods. There are too many methods to memorize in your first 100 days of Swift. Get comfortable with looking up method names in Apple's documentation. Next get comfortable with the idea that type methods work on types, not variables! See if the following example makes sense to you?
What methods can you call on an array of Integers? You have several in your code snip above. You've used filter, sorted, and map methods. Each of these methods work with an array of Integers. (Type = [Int] that is, an array of Integers) Now, the last step. Get comfortable with chaining functions together. When you filter lucky numbers, Swift returns an array of Integers. Well you know you can sort an array of integers, so just add the sorted() method.
The sorted function works on an array of integers, so you can add another function to the returned results.
To be honest, I write out my code just as you did. First to get it working. After it works, I see if I can reduce multi-line complexity. You can see a pattern in your code. You perform a simple function on an array of Integers. You give the results a simple name. Then you use that new variable in the very next line; often that's the LAST TIME you use that variable. When you see that pattern, ask yourself if you can just copy/ paste the function into one line, and skip over the whole "create a new, temporary variable" step.
|
|
Extra Credit Challenge Here's another challenge for you. Use as many temporary variables as you need to get this to work. Then figure out how to chain the methods together. Can you find the solution with one line of code? Practice determining what the types are. Also, you may have to look in Apple's documentation for helpful methods. Here's a string
Use three methods to isolate the heart character. Do not use String's substring function. Please write your answer below! |
|
Hi You can havve mutliple function on a variable So will give the result only odd numbers in the same order.
Add
Add
To print you need to go over the array
However there is a function
However as this is very long and hard to read you will see people tend to put each one on different lines but still is the same function call
|
|
Thank you so much for the help @NigelGee and @Obelix !! That all makes a lot of sense.
|
|
@obelix - thanks for the extra credit challenge. It was helpful to dig in more to chaining methods.
|
|
Since I did checkpoint 5 again yesterday and I came across this I thought I'd give the extra credit challenge a shot. This worked for me but I'm wondering what Obelix had planned for his solution.
|
|
@michaelpgalen and @vtabmow Well done! I think I had the first solution in mind, sorting then grabbing the first character. But well done for thinking out of the box by using dropFirst and dropLast. To be honest, those two methods weren't in my toolbox! I've never used them before. Well done! |
|
I was able to finish this but getting () at the end of the print statements.
|
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.
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.