All I did was to add the capital cases of the characters to the list. I was just wondering if there was a “better” or “more professional” way of solving this challenge?
const val TAVERN_NAME = "Taernyl's Folly"
fun main(args: Array<String>) {
placeOrder("shandy,Dragon's Breath,5.91")
//placeOrder("elixir,Shirley's Temple,4.12")
// Challenge
val capitalPhrase = "DRAGON'S BREATH: IT'S GOT WHAT ADVENTURERS CRAVE!"
println(toDragonSpeak(capitalPhrase))
}
private fun toDragonSpeak(phrase: String) =
phrase.replace(Regex("[aeiouAEIOU]")) {
when (it.value) {
"a", "A" -> "4"
"e", "E" -> "3"
"i", "I" -> "1"
"o", "O" -> "0"
"u", "U" -> "|_|"
else -> it.value
}
}
private fun placeOrder(menuData: String) {
val indexOfApostrophe = TAVERN_NAME.indexOf('\'')
val tavernMaster = TAVERN_NAME.substring(0 until indexOfApostrophe)
println("Madrigal speaks with $tavernMaster about their order.")
val data = menuData.split(',')
val type = data[0]
val name = data[1]
val price = data[2]
val message = "Madrigal buys a $name ($type) for $price."
println(message)
val phrase = if (name == "Dragon's Breath") {
"Madrigal exclaims: ${toDragonSpeak("Ah, delicious $name!")}"
} else {
"Madrigal says: Thanks for the $name."
}
println(phrase)
}
Use the constructor Regex(pattern: String, option: RegexOption). Use ignore case for the option so that both lowercase and uppercase will match. Next, convert it to lowercase.