Skip to content

Instantly share code, notes, and snippets.

@georgybu
Last active August 21, 2017 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgybu/942afa4bd44eea3ce5fcc9301c010eda to your computer and use it in GitHub Desktop.
Save georgybu/942afa4bd44eea3ce5fcc9301c010eda to your computer and use it in GitHub Desktop.
jsninja-BC1-2017-1
function drawNestedSetsTree(data, node) {
if (Array.isArray(data) && node && data.length > 0) {
data.sort((a, b) => a.left - b.left);
let minLeft = 0;
const buildElement = (data, node, parent = null) => {
if (Array.isArray(data)) {
data.forEach((item, index) => {
if ((!parent && index === 0) || (parent && item.left > minLeft && item.left > parent.left && item.right < parent.right)) {
const li = document.createElement('li');
li.innerHTML = item.title;
if (item.right - item.left > 2) {
const ul = buildElement(data, document.createElement('ul'), item);
if (ul.childNodes.length > 0) {
li.appendChild(ul);
}
}
minLeft = item.right;
node.appendChild(li);
}
});
}
return node;
};
node.appendChild(buildElement(data, document.createElement('ul')));
}
}
if (typeof module !== 'undefined') {
module.exports = drawNestedSetsTree;
}
@jsninjabot
Copy link

К сожалению неверно. Входные данные:

[ { title: 'yellow-zebra', left: 9, right: 10 },
  { title: 'sick-prose', left: 5, right: 12 },
  { title: 'temporary-lake', left: 3, right: 4 },
  { title: 'icky-humor', left: 17, right: 18 },
  { title: 'mucho-actor', left: 14, right: 15 },
  { title: 'majestic-existence', left: 1, right: 26 },
  { title: 'scared-grandmother', left: 16, right: 23 },
  { title: 'tropical-coal', left: 7, right: 8 },
  { title: 'natural-part', left: 6, right: 11 },
  { title: 'lousy-metal', left: 21, right: 22 },
  { title: 'agile-grain', left: 2, right: 25 },
  { title: 'deluxe-pizzas', left: 19, right: 20 },
  { title: 'defective-bee', left: 13, right: 24 } ]

Ожидаемый результат:

<ul><li>majestic-existence<ul><li>agile-grain<ul><li>temporary-lake</li><li>sick-prose<ul><li>natural-part<ul><li>tropical-coal</li><li>yellow-zebra</li></ul></li></ul></li><li>defective-bee<ul><li>mucho-actor</li><li>scared-grandmother<ul><li>icky-humor</li><li>deluxe-pizzas</li><li>lousy-metal</li></ul></li></ul></li></ul></li></ul></li></ul>

Полученный результат:

<ul><li>majestic-existence<ul><li>agile-grain<ul><li>temporary-lake</li></ul></li></ul></li></ul>

Пожалуйста, пришлите еще одно письмо после исправления проблемы

@jsninjabot
Copy link

Приветствую!
Поздравляю, ваш код прошел все наши тесты.
Пожалуйста, заполните форму регистрации на курс:
https://goo.gl/BEq8sP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment