Guest User

#100DaysOfSwiftUI - Day 8, Checkpoint 4

a guest
Nov 4th, 2026
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.89 KB | Source Code | 0 0
  1. import Playgrounds
  2.  
  3. #Playground {
  4.     enum SquareRootError: Error {
  5.         case outOfBounds, noRoot
  6.     }
  7.    
  8.     func mySquareRoot(_ number: Int) throws -> Int {
  9.         if number < 1 || number > 10_000 {
  10.             throw SquareRootError.outOfBounds
  11.         }
  12.         for i in 1...100 {
  13.             if i * i == number {
  14.                 return i
  15.             }
  16.         }
  17.        
  18.         throw SquareRootError.noRoot
  19.     }
  20.    
  21.    
  22.     let number = 10_000
  23.     do {
  24.         let answer = try mySquareRoot(number)
  25.         print("Your answer is \(answer)")
  26.     }catch SquareRootError.outOfBounds {
  27.         print("This function only supports numbers between 1 and 10,000")
  28.     }catch SquareRootError.noRoot {
  29.         print("We couldn't find the square root of the number you have provided. Sorry.")
  30.     }catch {
  31.         print("Something we haven't foreseen. You did it!")
  32.     }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment