Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

luke jones
luke jones
8,915 Points

Testing in playground with switch statements

func lunchSpecial (name: String, day: String) -> String {
    switch lunchSpecial (day, desert) {
    case "Monday" : "Chocolate"
    case "Tuesday" : "Toffee"
    case "Wednesday" : "Apple Pie"
    case "Thursday" : "Strawberry Shortcake"
    default: "No special today"
    }
    return "Hi \(name), \(day)'s lunch special is \(desert)"
}

lunchSpecial("Luke", day: "Monday")

Was reading the swift book then started to develop my own code in the playground. What's wrong here? How do i get desert to be displayed based on what day it is rather than having to enter the desert? Hope what i'm trying to do makes sense here for the reader.

Also, is there an easier/better way to write this with say enums?

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hey Luke,

Here's how I would do this:

func lunchSpecial (name: String, day: String) -> String {

    let desert: String

    switch day {
    case "Monday" :
        desert = "Chocolate"
    case "Tuesday" :
        desert = "Toffee"
    case "Wednesday" :
        desert = "Apple Pie"
    case "Thursday" :
        desert = "Strawberry Shortcake"
    default:
        return "Sorry, \(name). No special today."
    }

    return "Hi \(name), \(day)'s lunch special is \(desert)."

}

lunchSpecial("Luke", day: "Monday")
luke jones
luke jones
8,915 Points

Ahh that's perfect, thanks!