BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SOLVED: Question about Checkpoint 4

Forums > 100 Days of SwiftUI

@kib  

Hello, I just started the course 100 days of swiftUI and I have a question about what I did for Checkpoint 4 :

enum SqrtError: Error {
    case outOfBounds, noRoot
}

func calcSqrt(input: Int) throws -> Int {
    if(input < 1){
        throw SqrtError.outOfBounds
    }
    else if(input > 10_000){
        throw SqrtError.outOfBounds
    }
    else {
        for i in 1...100 {
            if(i*i==input)
            {
                return i
            }
            else if(i*i>input)
            {
                throw SqrtError.noRoot
            }
        }
    }
    return input //what do I need to return here
}

do {
    try print(calcSqrt(input: 60))
    }
    catch {
        print("Error: \(error)")
    }

As my comment "what do I need to return here" said I don't know what I should return so I guess my code is wrong at some point but I don't know where, does someone has some hint ? Thanks you in advance!

   

@kib asks about function return values:

return input //what do I need to return here

Welcome to HackingWithSwift

This is a great place to start your Swift learning journey. There are many great lessons ahead and this forum has plenty of folks who like to help either by simply giving you the direct answer, by giving hints to help you learn, and all combinations inbetween.

The Direct Answer

When you define a function that throws or returns a value, the Swift interpreter holds you firmly to that contract. So while you believe you covered all the if and else cases, it seems the compiler disagrees. To find out why, be the compiler.

//                   ๐Ÿ‘‡๐Ÿผ Check all values 1 through 100
        for i in 1...100 {
            if(i*i==input)
            {
            //    ๐Ÿ‘‡๐Ÿผ You found the root value 
                return i
            }
            else if(i*i>input)
            {
            //                 ๐Ÿ‘‡๐Ÿผ You prove i * i is greater than input value
                throw SqrtError.noRoot
            }
        }
        // If you're here, what happened?
        // But what is returned if neither of these cases is true?

Is there a case where i * i is not equal to the input value AND i * i is NOT greater than the input value?

Your mental math may say this will never happen, but the compiler insists you explicitly state this in code!

But rather than returning a nonsense number, such as -3, this is a great place to extend your SqrtError codes.

// Consider adding another error code
enum SqrtError: Error {
//                               ๐Ÿ‘‡๐Ÿผ This code will help you debug logic errors in future revisions
    case outOfBounds, noRoot, logicError}

//. ........ snip .........
//                   ๐Ÿ‘‡๐Ÿผ Check all values 1 through 100
        for i in 1...100 {
            //                 ๐Ÿ‘‡๐Ÿผ You found the root value 
            if(i*i==input) {  return i }

            //                                ๐Ÿ‘‡๐Ÿผ You prove i * i is greater than input value
            else if(i*i>input) { throw SqrtError.noRoot }
        }
        // If you're here, what happened?
        //                 ๐Ÿ‘‡๐Ÿผ Your code encountered a logic error! 
        throw SqrtError.logicError

Keep Coding

2      

@kib  

Oh nice, thanks for the answer, I never would have thought of doing that!

   

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.