Create HTML Tags

Report a typo

Implement the body of the function doInit() so that all children tags are stored in a parent tag.

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)
}
___

Create a free account to access the full topic