mardi 25 septembre 2018

Swift skips an "else if"

This Swift (xCode) ChatBot program correctly returns the "hello there" and "where are the cookies?" strings, but returns "could be anything" for the middle two that should be "To the North!". I thought it was a breakpoint or issue with a string on multiple lines, but this happens in both Xcode workplace and playground:

  struct MyQuestionAnswerer {
    func responseTo(question: String) -> String {

        let question = question.lowercased()

        let defaultNumber = question.count % 3

        if question == "hello there" {
            return "Why hello there"
        } else if question == "where should I go on holiday?" {
            return "To the North!"
        } else if question == "where can I find the north pole?" {
            return "To the North!"
        } else if question == "where are the cookies?" {
            return "In the cookie jar!"
        } else if defaultNumber == 0 {
            return "That really depends"
        } else if defaultNumber == 1 {
            return "Ask me again tomorrow"
        } else {
            return "Could be anything"
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire