Implement a function generateTOC(root) that constructs a Table of Contents (TOC) from an HTML document.
The function should:
level (heading level) and text (heading content).Examples
const doc = new DOMParser().parseFromString(` <div> <h1>Introduction</h1> <h2>Background</h2> <h2>Goals</h2> <h1>Conclusion</h1> </div>`, 'text/html'); generateTOC(doc.body); // [ // { level: 1, text: 'Introduction' }, // { level: 2, text: 'Background' }, // { level: 2, text: 'Goals' }, // { level: 1, text: 'Conclusion' } // ]
JavaScript Function
No test results yet
Click "Run" to execute tests