|
Can anyone help me with my code for Checkpoint 4? Swift is telling me I need to include a return in my function, but I already have one. Do I have it in the wrong place? What else have I done wrong? Appreciate any advice. Andrew |
|
Hint 1: Hint 2:
Hint 3: Hint 4: Number may not be a good name for your variable. Swift actually has a class named Refactor your code, you might get better results! If not repost your code here, using the code tags, of course. |
|
Thanks for your reply @Obelix. Here's my revised code.
It now works, but I'm still left with my original question. Why did I have to return a blank string (or any other string for that matter) to get it to work when I was already returning a string in the if? Grateful for any response. Andrew |
|
Andrew is asking the right questions! He asked:
Return Type is StringYou declared your function must return a What gets returned here if girlfriend's age is 42?
Answer is nothing is returned. So the compiler will complain. To fix, you might return a blank string?
In short, none of your |
|
RefactoringTake a look at this section of code:
|
|
Naming Variables+1 for updating variable names. But you missed an opportunity. Your user wants to find the square root of some number. Let's call that: See if this is clearer to future you?
Get in the habit of using descriptive variable names. XCode's code completion helps you, because you won't have to type out long names; it will do that for you. But future you will appreciate you using descriptive names. CS193p MemorizeIf you have a look at Paul Hagerty's Swift course at Stanford University (CS193p), in his lecture for his card matching game he creates an index named: The whole series is excellent and adds perspective to @twostraws' excellent lectures. For convenience, here's the part where CS193p talks about descriptive variable names. Jump to 01:12:00 > Memorize Game Logic |
SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.