Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/>
</head>
<body>
<div id="tree"></div>
<script src="scripts/main.js"></script>
</body>
</html>
20 changes: 19 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +24 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runtime guard for data being null/undefined/non-object is in place and prevents Object.keys/iteration errors, which aligns with the checklist requirement to avoid crashes for invalid input.

}

const ul = document.createElement('ul');

for (const key in data) {
const li = document.createElement('li');

li.textContent = key;

createTree(li, data[key]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your recursive call createTree(li, data[key]) correctly treats each li as the parent container for potential child lists, so nested ul elements are only added when there is deeper data, matching the desired leaf-node behavior.


ul.append(li);
}

if (ul.children.length > 0) {
element.append(ul);
Comment on lines +40 to +41

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appending the ul only when it has children ensures you don’t create empty lists for leaf nodes; this satisfies the requirement that li elements always exist while nested ul elements are conditional on having child keys.

}
}

createTree(tree, food);
Loading