Skip to content
Open

Develop #1758

Show file tree
Hide file tree
Changes from 8 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://PAVLO111.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
84 changes: 83 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,89 @@ const food = {
const tree = document.querySelector('#tree');

function createTree(element, data) {
// WRITE YOUR CODE HERE
for (const key in data) {
const firstList = document.createElement('li');

firstList.textContent = key;

// element.append(firstList);
document.body.append(firstList);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Items are appended to document.body instead of the element parameter. The container should receive the tree structure, not document.body. Uncomment element.append(firstList) and remove line 30.


const testLength = Object.keys(data[key]).length;

// console.log(testLength);

if (testLength > 0) {
const ul = document.createElement('ul');

firstList.append(ul);

createTree(null, 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.

The recursive call passes null instead of the ul element. When there are nested objects, you need to pass the newly created ul so the recursive call appends nested items to it. Change createTree(null, data[key]) to createTree(ul, 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.

Missing ul creation for the container. According to the requirements, you should create a ul element, append it to element, and then append li items to that ul. The commented code at the bottom shows this pattern correctly.

return element;
}

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 final tree is never appended to the tree element. Consider structuring the function so the initial call properly builds and appends to the element parameter.

// if (tree) {
// createTree(tree, food);
// }

createTree(tree, food);

// ====

// 'use strict';

// const food = {
// Drink: {
// Wine: {},
// Schnaps: {},
// },
// Fruit: {
// Red: {
// Cherry: {},
// Strawberry: {},
// },
// Yellow: {
// Banana: {},
// Pineapple: {},
// },
// },
// };

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

// function createTree(element, data) {
// // Твій основний цикл, який ти писав спочатку
// for (const key in data) {
// const firstList = document.createElement('li');

// firstList.textContent = key;

// // Додаємо пункт у поточний список (element)
// element.append(firstList);

// // Перевіряємо, чи є вкладені елементи
// const test = Object.keys(data[key]).length;

// if (test > 0) {
// // Якщо є вкладеність, створюємо НОВИЙ список для цього рівня
// const nestedUl = document.createElement('ul');

// // Додаємо цей список всередину нашого li
// firstList.append(nestedUl);

// // Викликаємо функцію знову для вкладеного списку
// // Це і є та сама рекурсія, яка замінює твій цикл "next"
// createTree(nestedUl, data[key]);
// }
// }
// }

// // Запускаємо один раз для головного контейнера
// // if (tree) {
// // createTree(tree, food);
// // }

// createTree(tree, food);
Loading