TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Day 8 Checkpoint 4 help

Forums > 100 Days of SwiftUI

@rcost  

Having some trouble with checkpoint 4. Do I even need to have specified errors for this if I am only throwing the one error message? What should I be returning at the bottom of this function to get rid of this error? Any help is appreciated!

2      

Hi! Possible solution can be like so:

enum RootError: Error {
    case outOfBounds, noRoot
}

func squareRoot(_ number: Int) throws -> String {
    // if number is out of bounds throw error
    if number > 10_000 || number < 1 {
        throw RootError.outOfBounds
    }

    // let's set the root as zero and find out the root of our number by looping through possible multiplication results
    var root = 0
    for i in 1...100 {
        if i * i == number {
            root = i
        }
    }

    // if root is still zero, meaning i * i never == number then throw error
    if root == 0 {
        throw RootError.noRoot
    }
    // return your result
    return "This is the square root: \(root)"
}

let test = 16
do {
    let result = try squareRoot(test)
    print(result)
} catch let error {
    print(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!

Reply to this topic…

You need to create an account or log in to reply.

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.