iOS Functions in Swift Adding Power to Functions Returning Complex Values

I need help with this Function Challenge

I believe this challenge is asking me to use a switch correct ? According to my code right now am I doing the correct thing ? I just got confused when they asked me to return 2 Double return types .

Please help

functions.swift
    func coordinate(for location :Stirng , cord: Double) ->Double {

        var cord =  0,0
        switch location {
        case "Eiffel Tower" : cord = 488582,22945
        case "Great Pyramid" : cord = 299792,311344
        case "Sydney Opera House" : cord = 338587,1512140
        default : cord = 0,0
        }

        return cord

    }

1 Answer

Greg Kaleka
Greg Kaleka
38,993 Points

Hi Jabor,

Let's go through the error message your code produces and fix each item:

Make sure your function is named coordinates

OK problem 1, your function name needs an "s" on the end. Fix that and submit again:

func coordinates(for location :Stirng , cord: Double) ->Double {
//...

Make sure you accept a single parameter of type String in your function declaration with the correct argument labels

OK, problem 2, you're accepting two parameters. We only need the name of the location! Also, you have a typo in "String" Fix those and submit again:

func coordinates(for location :String) ->Double {
//...

Your function needs to return a tuple containing two Double values

Oops! Only returning a single Double. Fix that and submit again:

func coordinates(for location :String) ->(Double, Double) {
//...

Your code could not be compiled. Please click on "Preview" to view the compiler errors.

Uh oh - let's see what the problem is. Ok, there's a problem with every comma. Oh yeah, tuples need parentheses. Let's add those and try again:

func coordinates(for location :String) ->(Double, Double) {

    var cord =  (0,0)
    switch location {
    case "Eiffel Tower" : cord = (488582,22945)
    case "Great Pyramid" : cord = (299792,311344)
    case "Sydney Opera House" : cord = (338587,1512140)
    default : cord = (0,0)
    }

    return cord

}

Your code could not be compiled. Please click on "Preview" to view the compiler errors.

Hm, what's the problem now? Let's look at the errors

error: cannot convert return expression of type '(Int, Int)' to return type '(Double, Double)'

Ah, whoops. Those coordinates are missing decimal places. We should also add decimal places to the cord definition:

func coordinates(for location :String) ->(Double, Double) {

    var cord = (0.0,0.0)
    switch location {
    case "Eiffel Tower" : cord = (48.8582,2.2945)
    case "Great Pyramid" : cord = (29.9792,31.1344)
    case "Sydney Opera House" : cord = (33.8587,151.2140)
    default : cord = (0,0)
    }

    return cord

}
  • Congrats! You completed the code challenge!

Woohoo we did it!

I hope this was helpful in showing you how to troubleshoot a code challenge (and code you write outside of code challenges as well!). You'll get better at spotting multiple issues at once, but it's very common for me still to go through a process like this one when tackling a bug.

A big shout out to Pasan Premaratne for writing such good error checking for this challenge - it's not always so easy to debug!

One last note - watch your formatting. Here's how I would write the code:

func coordinates(for location: String) -> (Double, Double) {

    var cord = (0.0, 0.0)
    switch location {
    case "Eiffel Tower":
        cord = (48.8582, 2.2945)
    case "Great Pyramid":
        cord = (29.9792, 31.1344)
    case "Sydney Opera House":
        cord = (33.8587, 151.2140)
    default:
        cord = (0, 0)
    }

    return cord

}

Cheers :beers:

-Greg