All About Birds
- Kinds of Birds
- The Finch
- The Swan
- Habitats
- Wetlands
- Kinds of Birds
- The Finch
- The Swan
- Habitats
- Wetlands
- Zoo
- Trees
const headingData = [
"H1 All About Birds",
"H2 Kinds of Birds",
"H3 The Finch",
"H3 The Swan",
"H2 Habitats",
"H3 Wetlands",
"H2 Kinds of Birds",
"H3 The Finch",
"H3 The Swan",
"H2 Habitats",
"H3 Wetlands",
"H4 Zoo",
"H4 Trees"
]
const Heading = function(weight, text) {
this.weight = weight;
this.text = text;
};
const Node = function(heading, children) {
this.heading = heading;
this.children = children;
};
function toOutline(headings) {
// Implement this function. Sample code below builds
// an outline of only the first heading
// let root = new Node(new Heading(0, ""), [new Node(headings[0], [])])
// return root
}
// Parses a line of the input
// This implementation is correct for all predefined test cases
function parse(record) {
const firstSpace = record.indexOf(" ");
const weight = parseInt(record.substr(1, firstSpace));
const text = record.substr(firstSpace + 1);
return new Heading(weight, text);
}
// Converts a node to HTML
function toHtml(node) {
let childHtml = "";
if (node.children.length > 0) {
childHtml = `<ol>${node.children.map(child => `<li>${toHtml(child)}</li>`).join("\n")}</ol>`;
}
const heading = node.heading.text.length === 0 ? "" : node.heading.text + "\n";
return heading + childHtml;
}
const headings = headingData.map(r => parse(r));
const outline = toOutline(headings);
console.log(toHtml(outline[0]));