HTML Serializer

Author
Engineering Manager, Robinhood
Languages

Given an object which resembles a DOM tree, implement a function that serializes the object into a formatted string with proper indentation (one tab (\t character) per nesting level) and one tag per line.

Examples

const tree = {
tag: 'body',
children: [
{ tag: 'div', children: [{ tag: 'span', children: ['foo', 'bar'] }] },
{ tag: 'div', children: ['baz'] },
],
};
serializeHTML(tree);
// Output:
`<body>
<div>
<span>
foo
bar
</span>
</div>
<div>
baz
</div>
</body>`;

Asked at these companies

Premium FeaturePurchase premium to see companies which ask this question.
View plans
The "Who Wrote This?" Tee — Every dev's inner monologueSponsor: SwagOverflow
The "Who Wrote This?" Tee — Every dev's inner monologue

Every dev's worst enemy? Their own code from a month ago. This black tee captures that moment of existential crisis when you debug your own work and wonder who could have written this mess, only to realize it was you. A must-have for front end engineers who know the struggle.

Get yours now ->

The "Who Wrote This?" Tee — Every dev's inner monologueSponsor: SwagOverflow
The "Who Wrote This?" Tee — Every dev's inner monologue

Every dev's worst enemy? Their own code from a month ago. This black tee captures that moment of existential crisis when you debug your own work and wonder who could have written this mess, only to realize it was you. A must-have for front end engineers who know the struggle.

Get yours now ->