Checkpoint 4

·

Write a function that takes an integer from 1 to 10000. Find the square root without using the sqrt function. Should include the error handling we’ve been working on.

My solution

enum numberCheck: Error {
    case lessThanOne, moreThanTenThousand
}

func someFunc(p1: Int) throws -> Int {
    if p1 < 1 {
        throw numberCheck.lessThanOne
    } else if p1 > 10000 {
        throw numberCheck.moreThanTenThousand
    }

    print("good num: \(p1)")
    for i in 1...100 {
        if i * i == p1 {
            return i
        }
    }

    return 0
}

do {
    var numToCheck = 3

    try someFunc(p1: numToCheck) != 0 ? print("The square root of \(numToCheck) is: \(someFunc(p1: numToCheck))") : print("the square root is not found for the number \(numToCheck)")

} catch {
    print("Num is out of bounds")
}

Currently
Building

1 product