Code:
fun main() {
val amanda = Person("Amanda", 33, "play tennis", null)
val atiqah = Person("Atiqah", 28, "climb", amanda)
amanda.showProfile()
atiqah.showProfile()
}
class Person(val name: String, val age: Int, val hobby: String?, val referrer: Person?) {
fun showProfile() {
fun reference(referrer: Person?): String {
return if(referrer!=null) "Has a referrer named ${referrer.name}, who likes to ${referrer.hobby}." else "Doesn't have a referrer.\n"
}
println("Name: $name\nAge: $age\nLikes to $hobby. \$${reference(referrer)}")
}
}
Expected output:
Name: Amanda
Age: 33
Likes to play tennis. Doesn't have a referrer.
Name: Atiqah
Age: 28
Likes to climb. Has a referrer named Amanda, who likes to play tennis.
Actual output:
Name: Amanda
Age: 33
Likes to play tennis. $Doesn't have a referrer.
Name: Atiqah
Age: 28
Likes to climb. $Has a referrer named Amanda, who likes to play tennis.
As you can see, I'm getting the '$' sign before the output of the function reference. Is there any way to remove it?
Got it! Just had to remove '$' in println before the function & voila! it works perfectly.