123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- // Рекурсия: HTML tree
- // Используя решение этого задания сделайте функцию, которая рекурсивно создает HTML-строку из древовидной
- // структуры данных Javascript любой вложенности. Проверьте результат работы функции выводя HTML-строку
- // используя document.write или же какой-то_элемент.innerHTML
- html_tree: {
- function htmlTree(element) {
- let str = '';
- let attrStr = '';
- if (element.attrs) {
- Object.keys(element.attrs).forEach(key => {
- attrStr += `${key}=${element.attrs[key]} `
- });
- }
- str += `<${element.tagName} ${attrStr}>`
- if (element.children) {
- for (let childIndex in Object.keys(element.children)) {
- if (element.children[childIndex].tagName) {
- str += htmlTree(element.children[childIndex]);
- } else {
- str += `${element.children}`;
- }
- }
- }
- str += `</${element.tagName}>`;
- return str;
- }
- const table = {
- tagName: 'table',
- attrs: {
- border: "1",
- },
- children: [
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["1x1"],
- },
- {
- tagName: "td",
- children: ["1x2"],
- },
- ]
- },
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["2x1"],
- },
- {
- tagName: "td",
- children: ["2x2"],
- },
- ]
- }
- ]
- }
- document.write(htmlTree(table));
- }
- // Рекурсия: DOM tree
- // Используя решение этого задания сделайте функцию, которая рекурсивно создает DOM из древовидной структуры
- // данных Javascript. Задание в целом аналогично предыдущему, однако вместо накопления результата в HTML-строке
- // функция должна добавлять элементы созданные через document.createElement в переданного в функцию родителя.
- dom_tree: {
- function domTree(parent, element) {
- let el = document.createElement(element.tagName);
- if (element.attrs) {
- Object.keys(element.attrs).forEach(key => {
- el[key] = element.attrs[key];
- });
- }
- parent.append(el);
- if (element.children) {
- for (let childIndex in Object.keys(element.children)) {
- if (element.children[childIndex].tagName) {
- domTree(el, element.children[childIndex]);
- } else {
- el.innerText = element.children;
- }
- }
- }
- }
- const table = {
- tagName: 'table',
- attrs: {
- border: "1",
- },
- children: [
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["1x1"],
- },
- {
- tagName: "td",
- children: ["1x2"],
- },
- ]
- },
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["2x1"],
- },
- {
- tagName: "td",
- children: ["2x2"],
- },
- ]
- }
- ]
- }
- domTree(document.body, table);
- }
- // Рекурсия: Deep Copy
- // Напишите функцию, которая рекурсивно осуществляет глубокое копирование структур Javascript, в которых
- // нет циклических связей.
- deep_copy: {
- const table = {
- tagName: 'table',
- attrs: {
- border: "1",
- },
- children: [
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["1x1"],
- },
- {
- tagName: "td",
- children: ["1x2"],
- },
- ]
- },
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["2x1"],
- },
- {
- tagName: "td",
- children: ["2x2"],
- },
- ]
- }
- ]
- }
- const arr = [1, "string", null, undefined, { a: 15, b: 10, c: [1, 2, 3, 4], d: undefined, e: true }, true, false]
- function deepCopy(obj) {
- let copy;
- if (Array.isArray(obj)) {
- copy = [];
- } else {
- copy = {};
- }
- if (obj) {
- Object.keys(obj).forEach(key => {
- if (typeof obj[key] == 'object') {
- copy[key] = deepCopy(obj[key]);
- } else {
- copy[key] = obj[key];
- }
- });
- }
- return copy;
- }
- const arr2 = deepCopy(arr); //arr2 и все его вложенные массивы и объекты - другие объекты, которые можно менять без риска поменять что-то в arr
- console.log(arr2);
- const table2 = deepCopy(table); //аналогично
- console.log(table2);
- }
- // Рекурсия: My Stringify
- // Напишите аналог JSON.stringify
- my_stringify: {
- function stringify(obj) {
- let str = '';
- let length = Array.isArray(obj) ? obj.length : Object.keys(obj).length;
- if (typeof obj == 'object') {
- str += Array.isArray(obj) ? '[' : '{';
- }
- Object.keys(obj).forEach((key, i) => {
- if (!Array.isArray(obj)) {
- str += `"${key}":`;
- }
- if (typeof obj[key] == 'object' && obj[key] != null) {
- str += stringify(obj[key]);
- } else if (typeof obj[key] == 'string') {
- str += `"${obj[key]}"`;
- } else if (obj[key] == undefined) {
- str += `null`;
- } else {
- str += `${obj[key]}`;
- }
- if (i != length - 1) {
- str += ',';
- }
- });
- if (typeof obj == 'object') {
- str += Array.isArray(obj) ? ']' : '}';
- }
- return str;
- }
- const arr = [1, "string", null, undefined, { a: 15, b: 10, c: [1, 2, 3, 4], d: undefined, e: true }, true, false];
- const jsonString = stringify(arr); //напишите функцию stringify без использования JSON.stringify
- console.log(jsonString);
- console.log(JSON.parse(jsonString));//не должно поломаться и должно вернуть структуру, во всем схожую с оригинальным arr или table
- }
- // Рекурсия: getElementById throw
- // Напишите функцию getElementById, которая будет аналогична document.getElementById. В качестве основы можете
- // взять материал лекции (walker), однако в цикл перебора children вставьте проверку на нахождение
- // переданного id. При нахождении элемента по id в рекурсивной функции выбрасывайте исключение со значением
- // найденного DOM-элемента, которое будет поймано на уровне вашей функции getElementById, после чего она
- // вернет выброшенный DOM-элемент.
- get_element_by_id: {
- function getElementById(idToFind) {
- function walker(parent = document.body) {
- for (const child of parent.children) {
- if (child.id == idToFind) {
- throw child;
- }
- walker(child);
- }
- }
- try {
- walker();
- } catch (e) {
- return e;
- }
- }
- console.log(getElementById('wantedParagraph'));
- }
|