From 7c04ec17feaf09ed8dcd27b181e802c6639f0299 Mon Sep 17 00:00:00 2001 From: Vita Martsekhovska Date: Thu, 21 May 2026 17:37:05 +0300 Subject: [PATCH] add task solution --- README.md | 6 +++--- src/index.html | 1 + src/scripts/main.js | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0adab1feb..0fef47b6d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 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 @@ -11,7 +11,7 @@ Use keys as list items. ![screenshot of the tree](example/object-tree.png) 1. Replace `` with your GitHub username in the link - - [DEMO LINK](https://.github.io/js_tree-from-object-DOM/) + - [DEMO LINK](https://vitamartsekhovska.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; diff --git a/src/index.html b/src/index.html index 3496ee135..17cc89176 100644 --- a/src/index.html +++ b/src/index.html @@ -9,6 +9,7 @@ /> +
diff --git a/src/scripts/main.js b/src/scripts/main.js index 2cdcd10cf..f28e5d9f8 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -21,7 +21,21 @@ const food = { const tree = document.querySelector('#tree'); function createTree(element, data) { - // WRITE YOUR CODE HERE + if (Object.keys(data).length === 0) { + return; + } + + const ourUl = document.createElement('ul'); + + for (const key in data) { + const ourLi = document.createElement('li'); + + ourLi.textContent = key; + createTree(ourLi, data[key]); + ourUl.append(ourLi); + } + + element.append(ourUl); } createTree(tree, food);