Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Playgrounds
- #Playground {
- enum SquareRootError: Error {
- case outOfBounds, noRoot
- }
- func mySquareRoot(_ number: Int) throws -> Int {
- if number < 1 || number > 10_000 {
- throw SquareRootError.outOfBounds
- }
- for i in 1...100 {
- if i * i == number {
- return i
- }
- }
- throw SquareRootError.noRoot
- }
- let number = 10_000
- do {
- let answer = try mySquareRoot(number)
- print("Your answer is \(answer)")
- }catch SquareRootError.outOfBounds {
- print("This function only supports numbers between 1 and 10,000")
- }catch SquareRootError.noRoot {
- print("We couldn't find the square root of the number you have provided. Sorry.")
- }catch {
- print("Something we haven't foreseen. You did it!")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment