0

Help

Complete the declaration of the getFirstAndLast function which is an extension of the String class with the return type Map <String, Char> that functions to get the first and last characters of the receiver's value. If run the console will display text like the following: First char Annabel is K and n for second letter First char Johnson is D and g for second letter ================ fun main() { val annabel = "Annabel".getFirstAndLast() val johnson = "Johnson".getFirstAndLast() val annabelFirstChar = annabel["first"] val annabelLastChar = annabel["last"] val johnsonFirstChar = johnson["first"] val johnsonLastChar = johnson["last"] println("First char Annabel is $annabelFirstChar and $annabelLastChar for second letter") println("First char Johnson is $johnsonFirstChar and $johnsonLastChar for second letter") } // TODO\ fun String.getFirstAndLast(): Map<String, Char>{ return mapOf() }

14th Jun 2020, 8:05 AM
Lady Gahwa
Lady Gahwa - avatar
1 Answer
+ 1
Not sure how "Annabel" has a "K" or "Johnson" has a "D" or "g". return mapOf("first" to this[0], "last" to this[this.length-1]) would make annabel["first"] == 'A' and annabel['last'] == 'l'. would make johnson["first"] == 'J' and johnson['last'] == 'n'.
15th Jun 2020, 3:22 AM
John Wells
John Wells - avatar