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
29 changes: 29 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm start & sleep 5 && npm test
- name: Upload tests report(cypress mochaawesome merged HTML report)
if: ${{ always() }}
uses: actions/upload-artifact@v2
with:
name: report
path: reports
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -11,7 +11,7 @@ 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://dianakomar.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;
Expand Down
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>
24 changes: 23 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,30 @@ const food = {

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

if (tree) {
tree.innerHTML = '';
}

function createTree(element, data) {
// WRITE YOUR CODE HERE
const ul = document.createElement('ul');

for (const key in data) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Using for..in will iterate inherited enumerable properties as well. Consider iterating only own keys, e.g. for (const key of Object.keys(data)) { ... } or check Object.prototype.hasOwnProperty.call(data, key) to be safer when building the tree from the object.

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

li.textContent = key;

const value = data[key];

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

createTree(tree, food);
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 function is invoked unconditionally here. If document.querySelector('#tree') returns null, calling createTree(tree, food) will throw when it tries to append the created

    . Wrap this call in the earlier if (tree) { ... } block or add a guard at the start of createTree to ensure element is a valid DOM node (per requirement that element must be a DOM element).

Loading