UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Checkpoint 4: Help handling Error from function.

Forums > 100 Days of SwiftUI

@Anas  

Hi there! In checkpoint 4, we're given the challenge of finding a root number without using 'sqrt()' function. One of the tasks is to return an error if you have't found a root no. after the loop has ended. I'm using a while loop and struggling to find a solution to return an error for 'noRoot' error. Any help would be greatly appreciated! Thanks :) (I've commented out my attempt in throwing noRoot error).

enum rootCause: Error {
    case outOfBounds, noRoot
}

var randomRoot = 0

func guessThePrime(myNumber: Int) throws {
    randomRoot = Int.random(in: 1...100)

    if myNumber == 1 {
        throw rootCause.outOfBounds
    }

//    for randomRoot in 1...100 {
//              if randomRoot * randomRoot != myNumber {
//                   throw rootCause.noRoot
//               }
//           }

    while randomRoot * randomRoot != myNumber {
        randomRoot = Int.random(in: 1...100)
        print("Still searching \(randomRoot)")
    }
}

do {
    try guessThePrime(myNumber: 25)

} catch rootCause.noRoot {
    print("There isn't an integer prime for this number!")

}
catch rootCause.outOfBounds {
    print("This is an Out of Bounds Number!")
}

2      

First, you've come to the right place for answers!

Next, you may have misinterpreted the instructions?

This is not a guessing game. I don't think you want to GUESS what the square root of a number is. Your code is supposed to figure it out exactly.

How would you figure out the square root of 25? (Your example?) Process of elimination? Maybe start by multiplying 1 by 1. Does this equal 25? No? What about 2 x 2? Is this 25? What about 3 x 3?

At some point you'll find a number that is the square root of 25, exactly. When you find it, you should return this number. But take a close look at your function's declaration. It does not return an integer!

But what if you hit a number when multiplied by itself is GREATER than the number you need a square root of?

What is the square root of 30? 5 x 5 is 25, so this is less than 30. But 6 x 6 is greater than 30.

So it seems there is no integer when multiplied by itself equals 30.

THIS is when you would throw a rootCause.noRoot.

Late edit. Removed my comment regarding naming Enums. It was late when I wrote my response. Anyway, that's my excuse and I'm sticking with it. Sorry for confusion.

3      

@Anas  

Hey Obelix! That makes more sense! Thanks for the help :) As for the enum, I've named it rootcause which conforms to the error protocol. Perhaps I'm mistaken but I'm not sure where I've named them error that'd cause confusion. Were you suggesting OutofBounds and noRoot be more descriptive? Thanks again Obelix! Appreciate the guidance which means I can attempt to code without a viewable swift solution!

2      

Doh!

You are 100% correct. I locked eyes on the Error keyword and got a bad case of code blindness. I've corrected my previous message. Sorry for confusion.

Can you show your corrected code? Would like to see how you implemented the outOfBounds error.

2      

Hacking with Swift is sponsored by RevenueCat

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.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.