Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am approving your solution because it correctly implements createTree(element, data) with recursion and produces the required nested <ul>/<li> structure for the provided food object, fulfilling the core task requirements. The main function works as expected for normal input and there are no critical blockers that would prevent the task from being considered complete.
There are, however, a few robustness improvements worth considering:
- In
src/scripts/main.js, avoid creating and appending an empty<ul>whendatais an empty object by checkingObject.keys(data).length > 0before creating the list. - Add a defensive check that
elementis actually a DOM element (for example,if (!(element instanceof Element)) throw new Error('element must be a DOM Element')) so invalid calls fail early with a clear message. - When recursing, guard against
nulland unintended arrays by usingif (data[key] && typeof data[key] === 'object' && !Array.isArray(data[key]))before callingcreateTreeagain.
These are not blockers for this assignment, but addressing them will make your code more robust and easier to maintain in real-world scenarios. Nicely done on implementing the recursive structure and satisfying the task’s main functional requirements.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
|
|
||
| function createTree(element, data) { | ||
| // WRITE YOUR CODE HERE | ||
| const ul = document.createElement('ul'); |
There was a problem hiding this comment.
You create the <ul> unconditionally. This can append an empty <ul> when data is an empty object. Consider creating/appending the <ul> only when Object.keys(data).length > 0 to avoid empty lists.
| // WRITE YOUR CODE HERE | ||
| const ul = document.createElement('ul'); | ||
|
|
||
| element.append(ul); |
There was a problem hiding this comment.
element.append(ul) assumes element is a DOM Element. The task requires element to be a DOM element — consider validating it (for example if (!(element instanceof Element)) throw new Error('element must be a DOM Element')) to fail early with a clear message.
| ul.append(newLi); | ||
| newLi.textContent = key; | ||
|
|
||
| if (typeof data[key] === 'object' && Object.keys(data[key]).length > 0) { |
There was a problem hiding this comment.
The condition typeof data[key] === 'object' is true for null (and for arrays). This can cause errors when data[key] is null. Use a safer check such as data[key] && typeof data[key] === 'object' and, if arrays are not intended, !Array.isArray(data[key]) before recursing.
No description provided.