|
Hi everybody! I am currently on Checkpoint 4 and there are some things I don't understand. I wrote a function that's supposed to return a square root of a given number, as well as handle errors that may occur. Here's my code:
The code sort of works. However, apart from printing the result, it also prints the number alone. It's understandable, since I wrote return myNumber, but I have no idea how to get rid of that part, so that only the square root is printed (as the function should return an integer). When I delete the return part, it gives me a warning. Also, is myNumber = 0 the only case when there is no root or am I missing something? I know these are probably very trivial things to ask, but I just didn't want to take an easy route and look for the whole solution. Thanks in advance! |
|
Stop coding for 10 minutes. Now try to find the square root of 9. Get out a piece of paper and write down the numbers from 1 to 12. Follow your logic for 1. Write down every decision your code makes. Follow your logic for 2. Write down every decision your code makes. Continue this paper trail for the first 12 numbers. Do you see any repetition? Do you see unnecessary decisions being made? Here's one... why are you attempting 11 * 11? This number is WAAAAAAY greater than 9! In this example you stopped at 12. But your code takes this all the way up to 10_000. What other problems did you uncover while running your square root solution on paper? Coding, whether in Swift, JAVA, C++, Basic, Fortran, Visual Basic, is a lot of puzzle solving. It's not your Swift that's the problem right now. It's the logic you're using to solve the puzzle. How would you describe this solution to a young student? We could just tell you the answer! But you should probably try to solve this one on your own. First, solve the puzzle. Then figure out the Swift loops, variables, returns, and print statements. We've all been in your shoes. We're here to answer more questions, but give this another try first. |
|
Well done you nearly there but this ( The only another thing is you check the number is in the range everytime you go though the loop, so you might want to think about a
The last thing is you return the orginal number! here your code amended slightly. hope this is clear.
|
|
You can limit the number of times you go through the central loop even more:
Without a |
|
Thank you so much for your replies. I @Obelix - thank you for giving me food for thought. I realized that my code ran up to 10_000 every single time, no matter if myNumber would be 1, 4, or 9999. Also, the line:
makes no sense, as 0 is out of the range I'm working with. I do not understand why it took me so long to see it! Thank you again for trying to change not only my code, but my line of reasoning. As I'm typing this, I noticed that there are another comments from @NigelGee and @roosterboy - now, with your improvements, it all makes sense. I hope one day I'll be able to help somebody here, just like you did. It's a loooong way to go, but also if you told me 3 weeks ago that I'd be writing pieces of code like this I wouldn't believe you. Have a great day and thanks for being so supportive ;) |
|
I'm trying to understand why, when I change:
to:
in my playground, it goes from working as expected to saying "The playground encountered a crash and could not finish executing." "0 is out of range." prints on the console. Xcode 14 beta 2, although I doubt that's relevant. Here's the rest of the code & thanks in advance for clues! :
|
|
Because the very first value in your
The
If the And then you get a crash here:
because when the
and that triggers an error since a range's lower bound must be smaller than its upper bound. And why are you dividing |
SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!
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.