Parting words

Report a typo

The function pepTalk takes the space traveler’s name in the input (two words separated by a space) and returns a string with their name and last-minute advice. If the name has an incorrect format, an exception may occur in pepTalk.

Fix the code below: correct the call of the function by wrapping it in the try-catch-finally expression.

If all goes well, the variable advice should be equal to the string that is the result of the execution of pepTalk.

If an exception occurs when calling the function, advice should contain the message Don't lose the towel, nameless one..

In the final block, write the code that will print Good luck!.

Sample Input 1:

Arthur Dent

Sample Output 1:

Good luck!
Don't lose the towel, traveler Arthur Dent!

Sample Input 2:

Ford

Sample Output 2:

Good luck!
Don't lose the towel, nameless one.
Write a program in Kotlin
fun pepTalk(name: String): String {
val array = name.split(" ").toTypedArray()
val firstName = array[0]
val secondName = array[1]
return "Don't lose the towel, traveler $firstName $secondName!"
}
// do not change the function above

fun main() {
val name = readLine()!!
val advice = pepTalk(name) // fix this function call
print(advice)
}
___

Create a free account to access the full topic