
Hi! I'm having some trouble with Checkpoint 4. I know it's simple but I'm getting quite confused. Could anybody evaluate my code and see if I'm on the right track? Here's my code:
Here are also a few issues that I'm encountering:


A new hacker considers their approach:
This is the power of function signatures!
Your function's signature defines what it will accept, what it will return, and whether it detects errors for you. Your signature tells any programmers using your function that you will only accept integers. Probably not necessary to check inside your code if, indeed, you received an Integer or a Double. Try it in Playgrounds!


New hacker has a missing variable.
Swift only holds onto the variables it needs. If you define a variable inside a scope, Swift will delete the variable once you destroy that scope. For your knowledge bank, you need to recognize where your scope begins and ends. Again, try this code snip in a Playground

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! 

I see
This is because we've indicated in our function testInteger that someNumber parameter should be of type Int
Just to clarify, so that in itself is already a validation right? This means that I no longer need to add it in our enums as it already handles any other inputs that is not an integer. This took me quite a while but I think this version of my updated code is a bit better (let me know if it indeed is or if I'm just feeling like it is but it is actually not)
I think this is still far from the proper answer but any guidance/clues would be really helpful. I feel like it's almost there and I'd definitely love it if I were to be able to figure the answer out on my own. Thanks hacker mentor! 

Because your function may throw an error, you need to attempt the function by using a
Are you surprised by the answer your code produces? 

Hey man. Sorry it took me a while. I tried to think as to why 25 returned no root and I'm feeling a bit stucked here. Shouldn't it display 5 since my current loop states that
The way I'm thinking is if we're looking for the square root of a number (let's say 25), it'll iterate from 1 to 10,000. If the value of the number that we're looking for matches the answer (25) we'll take i and display it as the square root. 25 = 5 x 5 therefore, since 25 (sqrtAns == number), i (5) would be the square root. I tried interchanging the if and else if statement but I'm not quite sure as to how to proceed any further. Any clues would be greatly appreciated. Thanks again. 

Try working through the logic of your loop one number at a time. You should soon see the problem. 

Looping from 1 to 10,000
I see. So it immediately throws the sqrtError.noRoot after the first iteration of the loop? Is my understanding correct? 

A lightbulb moment!
Exactly! Swift is doing exactly what you've told it to do. If the math doesn't add up, then you tell it to THROW AN ERROR. Perhaps you'll want to consider ALL the possibilities before you THROW AN ERROR. 

Just to be 100% sure, once it throws an error, it immediately exits the loop right? Right now, I figured out that I should add an additional condition just to make sure that it doesn't throw an error.
This means that if the sqrtAns is already bigger than the user's number (square root of the number that the user is looking for), then there's no way there could be a square root for that number. However, I've been racking my brain and upon running this code, it still returns a "There's no square root for that number" error. I've now updated my code into
The logic behind this is that as long as the number is not equal to sqrtAns and that the sqrtAns is not yet larger than the original number that the user is looking for the square root for, we will keep on running the for loop. However, once it matches, the square root would be i. I tried running this but it doesn't seem to run properly. Am I still missing something here?


Hi! Updating this thread for anyone in the future who'll probably encounter the same error as me. I've finally figured out the solution to this although it took me days to do so (I know it's that bad). I hope this will also be able to help you especially if you're also experiencing difficulties with this problem. I'll try to explain everything and be as comprehensive as possible as well.
We started by declaring a value for sqrtAns and set it into a default of 0. This will be a place holder for our sqrtAns (Final answer which solves the square root of whatever number we want) Let's start with the if else conditions here
We basically created two conditions which checks if the number that we're looking for is within the range of 1 to 10,000 (as per requirements) The next thing that we'll be doing is to calculate for the value of the square root. Here, we will be using a for loop to iterate over the values.
We divided the original number by 2 as it would make our loop exit faster (if there's no possible square root). The reason we did this is we wanted to minimize the number of iterations for our loop  which makes sense. If you're looking for the square root of a number, the answer cannot exceed the number itself. Additionally, the square root, cannot be more than half of the number itself as well since
This means that we can divide the number by two to further reduce the number of iterations that our for loop will have to do. To calculate for the square root, we initialized another integer named square and assigned it to the value of i x i (current index multiplied to itself) We will then compare if square matches the value of our number
I've added one last noRoot error outside our for loop just in case it finishes the loop and wasn't able to enter our if else statement.  This also means that there's no square root as it completed the for loop without even entering our if square == number statement. I hope my explanation was comprehensive enough and it provided clarity as to how the problem should be solved. Marking the thread as solved now. 
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.