-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Solution #1771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Solution #1771
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| /> | ||
| </head> | ||
| <body> | ||
| <div id="tree"></div> | ||
| <script src="scripts/main.js"></script> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,25 @@ const food = { | |
| const tree = document.querySelector('#tree'); | ||
|
|
||
| function createTree(element, data) { | ||
| // WRITE YOUR CODE HERE | ||
| if (!data || typeof data !== 'object') { | ||
| return; | ||
| } | ||
|
|
||
| const ul = document.createElement('ul'); | ||
|
|
||
| for (const key in data) { | ||
| const li = document.createElement('li'); | ||
|
|
||
| li.textContent = key; | ||
|
|
||
| createTree(li, data[key]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your recursive call |
||
|
|
||
| ul.append(li); | ||
| } | ||
|
|
||
| if (ul.children.length > 0) { | ||
| element.append(ul); | ||
|
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Appending the |
||
| } | ||
| } | ||
|
|
||
| createTree(tree, food); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The runtime guard for
databeing null/undefined/non-object is in place and preventsObject.keys/iteration errors, which aligns with the checklist requirement to avoid crashes for invalid input.