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

SOLVED: Question: CheckPoint 4 (throwing errors) Why my code not working?

Forums > 100 Days of SwiftUI

@boat  

Hello,

My code is very similar to Paul's solution , but mine is not working

here is :

enum squareRootError: Error{
    case outOfBounds, noRoot
}

func CheckPoint4 (checkNumber: Int) throws -> Int {
    if checkNumber < 1 || checkNumber > 10_000 {
    throw squareRootError.outOfBounds
}
     for i in 1...100 {
        if (i * i) == checkNumber {
            return i }
    }
    throw squareRootError.noRoot

let checkNumber = 6561

do {
    let rootNumber = try CheckPoint4 (checkNumber: 6561)
    print ("The square root of \(checkNumber) is \(rootNumber).")
}    catch {
    print ("sorry there was a problem")
}

Thank you in advance!

Boat

2      

You need a closing brace } after the line throw squareRootError.noRoot to end your CheckPoint4 function.

Try to keep your indentation and matching braces lined up correctly; that will help you catch things like this.

3      

If you select all the code and ctl+i will give you correct indents.

3      

@boat  

Thank you both @roosterboy and @@NigelGee

Don't know why I was so blind.

had been frustrated to see my code not working; now even more frustrated to realize I was blind .

LOL

Thank you guys and have a great Friday.

Boat

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.