Skip to content
Advertisement

Can’t stratify output based on different headings and their corresponding paragraphs

I’m trying to fetch each heading and their corresponding paragraphs from the html elements below. The results should be stored within a dictionary. Whatever I’ve tried so far produces ludicrously haphazard output. I intentionally did not paste the current output only because of brevity of space.

JavaScript

I’ve tried with (producing messy output):

JavaScript

Output I wish to get:

JavaScript

Advertisement

Answer

Tricky problem. I think you have to handle things linearly:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement