Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Tree from object
Write a function `createTree(element, data)` that creates a nested list of `ul/li` from an object.
Use keys as list items.

Write a function `createTree(element, data)` that creates a nested list of `ul/li` from an object.
Use keys as list items.

`element` - is a DOM element

Expand All @@ -11,13 +12,14 @@ Use keys as list items.
![screenshot of the tree](example/object-tree.png)

1. Replace `<your_account>` with your GitHub username in the link
- [DEMO LINK](https://<your_account>.github.io/js_tree-from-object-DOM/)
- [DEMO LINK](https://TIgor22.github.io/js_tree-from-object-DOM/)
2. Follow [this instructions](https://github.com/mate-academy/js_task-DOM-guideline)
- Run `npm run test` command to test your code;
- Run `npm run test:only -- -n` to run fast test ignoring linter;
- Run `npm run test:only -- -l` to run fast test with additional info in console, ignoring linter.
- Run `npm run test` command to test your code;
- Run `npm run test:only -- -n` to run fast test ignoring linter;
- Run `npm run test:only -- -l` to run fast test with additional info in console, ignoring linter.

### Styling with SCSS 🎨

This project uses Parcel, which automatically handles the compilation of SCSS to CSS out of the box.

You do not need to manually compile your .scss files. Simply link your main SCSS file directly in your HTML, and Parcel will take care of the rest.
40 changes: 38 additions & 2 deletions src/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,46 @@ const food = {
},
};

const tree = document.querySelector('#tree');
let tree = document.querySelector('#tree');

if (!tree) {
const container = document.createElement('div');

container.id = 'tree';
document.body.appendChild(container);
tree = container;
}

function buildList(obj) {
const ul = document.createElement('ul');

for (const key of Object.keys(obj)) {
const li = document.createElement('li');

li.textContent = key;

const value = obj[key];

if (
typeof value === 'object' &&
value !== null &&
Object.keys(value).length > 0
) {
li.appendChild(buildList(value));
}

ul.appendChild(li);
}

return ul;
}

function createTree(element, data) {
// WRITE YOUR CODE HERE
element.innerHTML = '';

const rootUI = buildList(data);

element.appendChild(rootUI);
}

createTree(tree, food);
Loading