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

Checkpoint 4 | Good Solution?

Forums > 100 Days of SwiftUI



```//I just finished the 4th Checkpoint and struggled a little bit with it. Here is my solution if you see any big mistakes please tell me. (The code is running but I don´t know of this is good code or bad?)

import UIKit

enum PasswordError: Error {
    case higherlower, nomatch
}

func root(_ Zahl: Int) throws -> String {
    for i in 1...100 {
        if Zahl < 1 || Zahl > 10000 {
            throw PasswordError.higherlower
        }
        if Zahl == i * i {
        return "root = \(i)"
        } else {
            throw PasswordError.nomatch
        }//if
    } //loop
    return "Help"
} //function

do {
    let result = try root(8)
    print("The root is \(result)")
} catch PasswordError.higherlower {
    print("Out of bounds")
} catch PasswordError.nomatch {
    print("No Root")
}

3      

Just a couple of points You have a check for the range inside the loop, but you only need to check once before the loop.

You could try using guard this tell people that this is check

you name should be camelCase so Zahl should be zahl and higherLower, noMatch

Also you have throw PasswordError.nomatch so this will exit after the first pass eg 1 * 1

The other thing I would do is instead of not having a external paramater _ use of

So this slight amended

enum PasswordError: Error {
    case higherLower, noMatch
}

func root(of zahl: Int) throws -> String {
    guard zahl > 1 && zahl < 10000 else {
        throw PasswordError.higherLower
    }

    for i in 1...100 {
        if zahl == i * i {
            return "\(i)"
        }
    }

    throw PasswordError.noMatch
}

do {
    let result = try root(of: 81)
    print("The root is \(result)")
} catch PasswordError.higherLower {
    print("Out of bounds")
} catch PasswordError.noMatch {
    print("No Root")
} catch {
    print("Help")
}

PS you also missed catch all

5      

maxi! Nice effort on your first try. You'll start to see ways to improve your code as you continue.

@nigel's suggestion of guard is an example!

I like to think of guards as soldiers guarding a Schloss, or border guards between two countries. (Grenzsoldat? is this the word?)

The guards stand at the gates of your function. The guards challenge any variables trying to enter. They demand to see passports and papers! If the variable fails the test, then the "guards" do not let it into the castle. You can't run the rest of the code because your variable wasn't let in through the gate.

In @nigel's example,

guard zahl > 1 && zahl < 10000 else {
    // You SHALL NOT PASS.
        throw PasswordError.higherLower
    }

Do not even consider trying any other code, if the number (zahl) is outside the given boundaries. STOP right there.

5      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.