Implement the body of the function doInit() so that all children tags are stored in a parent tag.
Lambda with receiver
Create HTML Tags
Report a typo
Sample Input 1:
Sample Output 1:
<table><tr><td></td><td></td></tr></table>Write a program in Kotlin
open class Tag(val name: String) {
val children = mutableListOf<Tag>()
fun <T : Tag> doInit(child: T, init: T.() -> Unit) {
// write your code here
}
override fun toString() =
"<$name>${children.joinToString("")}</$name>"
}
/* Do not change code below */
fun table(init: TABLE.() -> Unit): TABLE = TABLE().apply(init)
class TABLE : Tag("table") {
fun tr(init: TR.() -> Unit) = doInit(TR(), init)
}
class TR : Tag("tr") {
fun td(init: TD.() -> Unit) = doInit(TD(), init)
}
class TD : Tag("td")
fun main() {
val myTable = table {
tr {
for (i in 1..2) {
td {
}
}
}
}
println(myTable)
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.