Render Sidebar Labels as HTML
Updated
Problem statement
For this exercise, you are given a list of text labels in order. Build the HTML for a sidebar that shows them, and return it as one string.
- Start with exactly
<aside><ul>. - For each label, in the order given, add
<li>, then the escaped label, then</li>. - Finish with exactly
</ul></aside>.
To escape a label, replace & with &, < with <, and > with >. Keep every other character as it is, including quotes and spaces. Treat each label as plain text, so text that already looks like an HTML entity is escaped too.
- Keep the labels in order, and keep duplicate labels and empty labels.
- If the list is empty, return only the outer wrapper.
- Do not add extra whitespace, attributes, links, styling or routing behavior.
Function
renderSidebar(labels: String[]) → StringExamples
Example 1
labels = ["One","Two"]return = "<aside><ul><li>One</li><li>Two</li></ul></aside>"The output opens the wrapper, turns One and Two into one list item each, in that order, and then closes the wrapper.
Example 2
labels = ["A&B","<tag>"]return = "<aside><ul><li>A&B</li><li><tag></li></ul></aside>"In the first label A&B, the ampersand becomes &. In the second label, the angle brackets become < and >, so the tag shows as text instead of real markup. Only the fixed wrapper adds real HTML tags.
Constraints
0 <= labels.length <= 1000.- Every label uses only printable ASCII characters, with codes from
32through126. Labels may be empty. - The sum of all label lengths is at most
1000.