main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. let a = {
  2. name: 'Ivan',
  3. surname: 'Ivanov',
  4. };
  5. let b = {
  6. name: 'Ivan',
  7. surname: 'Ivanov',
  8. };
  9. let c = {
  10. name: 'Ivan',
  11. surname: 'Ivanov',
  12. };
  13. function runDifferentFields() {
  14. a.age = 25;
  15. b.fathername = 'Ivanovich';
  16. c.sex = 'man';
  17. }
  18. function runFieldsCheck() {
  19. a.age = 25;
  20. b.fathername = 'Ivanovich';
  21. c.sex = 'man';
  22. alert('age' in a);
  23. if ('fathername' in b) {
  24. alert(`${b.fathername}`);
  25. }
  26. if ('sex' in c) {
  27. alert(`${c.sex}`);
  28. }
  29. }
  30. function runArrayOfPersons() {
  31. let persons = [];
  32. persons.push(b);
  33. persons[1] = {name: 'Ivan', surname: 'Ivanov',};
  34. persons.push(c);
  35. console.log(persons);
  36. }
  37. function runLoopOfPersons() {
  38. let persons = [];
  39. persons.push(b);
  40. persons[1] = {name: 'Ivan', surname: 'Ivanov',};
  41. persons.push(c);
  42. for (let i = 0; i < persons.length; i++) {
  43. console.log(persons[i]);
  44. }
  45. }
  46. let originPersons = [
  47. {
  48. name: 'Egor',
  49. surname: 'Egorov',
  50. age: 24,
  51. isCleanShaven: true,
  52. },
  53. {
  54. name: 'Oleg',
  55. surname: 'Olegov',
  56. age: 29,
  57. isBeard: true,
  58. },
  59. {
  60. name: 'Yriy',
  61. surname: 'Yriyev',
  62. age: 45,
  63. isMarried: false,
  64. isAlcoholic: true,
  65. hasCovid: true,
  66. }
  67. ];
  68. function runLoopOfNameAndSurname() {
  69. let persons = [...originPersons];
  70. for (let i = 0; i < persons.length; i++) {
  71. console.log(persons[i].name, persons[i].surname);
  72. }
  73. }
  74. function runLoopOfLoopOfValues() {
  75. let persons = [...originPersons];
  76. for (let i = 0; i < persons.length; i++) {
  77. let person = persons[i];
  78. for (let k = 0; k < 1; k++) {
  79. let result = Object.entries(person);
  80. console.log(result);
  81. }
  82. }
  83. }
  84. function runFullName() {
  85. let persons = [...originPersons];
  86. persons[0].fathername = 'Egorovich';
  87. persons[1].fathername = 'Olegovich';
  88. console.log(persons);
  89. for (let i = 0; i < persons.length; i++) {
  90. persons[i].fullname = `${persons[i].surname} ${persons[i].name} ${persons[i].fathername}`;
  91. }
  92. }
  93. function runSerialize() {
  94. let persons = [...originPersons];
  95. let resultJSONStringify = JSON.stringify(persons);
  96. console.log(resultJSONStringify);
  97. }
  98. function runDeserialize() {
  99. let persons = [...originPersons];
  100. persons[2] = JSON.parse('{"name":"Alyona","surname":"Ivanova","age":"23"}');
  101. console.log(persons);
  102. }
  103. function runHTML() {
  104. let persons = [...originPersons];
  105. let str = '<table>';
  106. for (let i = 0; i < 2; i++) {
  107. person = persons[i];
  108. let personName = person.name;
  109. let personSurName = person.surname;
  110. let personAge = person.age;
  111. str += '<tr>';
  112. for (let k = 0; k < 1; k++) {
  113. str += `<td>Имя:</td><td>${personName}</td>`;
  114. str += `<td>Фамилия:</td><td>${personSurName}</td>`;
  115. str += `<td>Возраст:</td><td>${personAge} лет</td>`;
  116. }
  117. str += '</tr>';
  118. }
  119. str += '</table>';
  120. document.write(str);
  121. }
  122. function runHTMLOptionalFields() {
  123. let persons = [
  124. {
  125. name: 'Egor',
  126. surname: 'Egorov',
  127. age: 24,
  128. isMarried: true,
  129. isAlcoholic: false,
  130. isCleanShaven: true,
  131. },
  132. {
  133. name: 'Oleg',
  134. surname: 'Olegov',
  135. age: 29,
  136. isMarried: false,
  137. isAlcoholic: true,
  138. isCleanShaven: false,
  139. },
  140. {
  141. name: 'Yriy',
  142. surname: 'Yriyev',
  143. age: 45,
  144. isMarried: false,
  145. isAlcoholic: true,
  146. isCleanShaven: false,
  147. }
  148. ];
  149. let str = '<table border="1">';
  150. for (let i = 0; i < 2; i++) {
  151. person = persons[i];
  152. let personName = person.name;
  153. let personSurName = person.surname;
  154. let personAge = person.age;
  155. let personIsMarried = person.age;
  156. let personIsAlcoholic = person.isAlcoholic;
  157. let personIsCleanShaven = person.isAlcoholic;
  158. str += '<tr>';
  159. for (let k = 0; k < 1; k++) {
  160. str += `<td>Имя:</td><td>${personName}</td>`;
  161. str += `<td>Фамилия:</td><td>${personSurName}</td>`;
  162. str += `<td>Возраст:</td><td>${personAge} лет</td>`;
  163. str += `<td>Увлекается алкоголем:</td><td>${personIsAlcoholic}</td>`;
  164. str += `<td>Женат:</td><td>${personIsMarried}</td>`;
  165. str += `<td>Выбрит идеально гладно:</td><td>${personIsCleanShaven}</td>`;
  166. }
  167. str += '</tr>';
  168. }
  169. str += '</table>';
  170. document.write(str);
  171. }
  172. function runHTMLTrColor() {
  173. let persons = [
  174. {
  175. name: 'Egor',
  176. surname: 'Egorov',
  177. age: 24,
  178. isMarried: true,
  179. isAlcoholic: false,
  180. isCleanShaven: true,
  181. },
  182. {
  183. name: 'Oleg',
  184. surname: 'Olegov',
  185. age: 29,
  186. isMarried: false,
  187. isAlcoholic: true,
  188. isCleanShaven: false,
  189. },
  190. {
  191. name: 'Yriy',
  192. surname: 'Yriyev',
  193. age: 45,
  194. isMarried: false,
  195. isAlcoholic: true,
  196. isCleanShaven: false,
  197. }
  198. ];
  199. let str = '<table border="1">';
  200. for (let i = 0; i < 2; i++) {
  201. person = persons[i];
  202. let personName = person.name;
  203. let personSurName = person.surname;
  204. let personAge = person.age;
  205. let personIsMarried = person.age;
  206. let personIsAlcoholic = person.isAlcoholic;
  207. let personIsCleanShaven = person.isAlcoholic;
  208. str += '<tr style="color: red">';
  209. for (let k = 0; k < 1; k++) {
  210. str += `<td>Имя:</td><td>${personName}</td>`;
  211. str += `<td>Фамилия:</td><td>${personSurName}</td>`;
  212. str += `<td>Возраст:</td><td>${personAge} лет</td>`;
  213. str += `<td>Увлекается алкоголем:</td><td>${personIsAlcoholic}</td>`;
  214. str += `<td>Женат:</td><td>${personIsMarried}</td>`;
  215. str += `<td>Выбрит идеально гладно:</td><td>${personIsCleanShaven}</td>`;
  216. }
  217. str += '</tr>';
  218. }
  219. str += '</table>';
  220. document.write(str);
  221. }
  222. function runHTMLThOptional() {
  223. let persons = [
  224. {
  225. name: 'Egor',
  226. surname: 'Egorov',
  227. age: 24,
  228. isMarried: true,
  229. isAlcoholic: false,
  230. hasCovid: true,
  231. },
  232. {
  233. name: 'Oleg',
  234. surname: 'Olegov',
  235. age: 29,
  236. isMarried: false,
  237. isAlcoholic: true,
  238. isCleanShaven: false,
  239. eyesColor: 'blue',
  240. },
  241. {
  242. name: 'Yriy',
  243. surname: 'Yriyev',
  244. age: 45,
  245. isMarried: false,
  246. likeHealthyFood: true,
  247. isCleanShaven: false,
  248. }
  249. ];
  250. const keys = persons.map(Object.keys);
  251. const withoutDuplicate = keys.flat();
  252. const setKeys = new Set(withoutDuplicate);
  253. const arrOfKeys = [...setKeys];
  254. let str = '<table border="1">';
  255. arrOfKeys.forEach(key => {
  256. str += `<th>${key}</th>`;
  257. });
  258. persons.forEach(person => {
  259. str += `<tr>`;
  260. console.log(person);
  261. arrOfKeys.forEach(key => {
  262. let result;
  263. if (person[key] === true) {
  264. result = 'да';
  265. } else if (person[key] === false) {
  266. result = 'нет';
  267. } else if (person[key] === undefined) {
  268. result = '';
  269. } else {
  270. result = person[key];
  271. }
  272. str += `<td>${result}</td>`;
  273. });
  274. str += `</tr>`;
  275. });
  276. str += '</table>';
  277. document.write(str);
  278. }
  279. function runDestructArray() {
  280. let arr = [1, 2, 3, 4, 5, 'a', 'b', 'c'];
  281. let resultEven = arr.filter(even => {
  282. return even % 3;
  283. });
  284. const [even1, even2] = resultEven;
  285. console.log(even1, even2);
  286. let resultOdd = arr.filter(odd => {
  287. return odd % 2;
  288. });
  289. const [odd1, odd2, odd3] = resultOdd;
  290. console.log(odd1, odd2, odd3);
  291. let resultLetters = arr.filter(letter => {
  292. return typeof letter === 'string';
  293. });
  294. const [letter1, letter2, letter3] = resultLetters;
  295. console.log(letter1, letter2, letter3);
  296. }
  297. function runDestructString() {
  298. let arr = [1, 'abc'];
  299. const letters = arr[1].split('');
  300. arr.pop();
  301. for (let i = 0; i < letters.length; i++) {
  302. arr.push(letters[i]);
  303. }
  304. let [number, s1, s2, s3] = arr;
  305. console.log(arr);
  306. }
  307. function runDestruct2() {
  308. let obj = {
  309. name: 'Ivan',
  310. surname: 'Petrov',
  311. children: [
  312. {name: 'Maria'},
  313. {name: 'Nikolay'}]
  314. };
  315. const [{name: name1}, {name: name2}] = obj.children;
  316. console.log(name1, name2);
  317. }
  318. function runDestruct3() {
  319. let arr = [1, 2, 3, 4, 5, 6, 7, 10];
  320. const {0: a, 1: b, length} = arr;
  321. console.log(a, b, length);
  322. }
  323. function runBlueBelt() {
  324. let someTree = {
  325. tagName: 'table',
  326. subTags: [
  327. {
  328. tagName: 'tr',
  329. subTags: [
  330. {
  331. tagName: 'td',
  332. text: 'some text',
  333. },
  334. {
  335. tagName: 'td',
  336. text: 'some text 2',
  337. }
  338. ]
  339. }
  340. ],
  341. attrs:
  342. {
  343. border: 1,
  344. },
  345. };
  346. function createComponent(object) {
  347. let parentElem = document.createElement(object.tagName);
  348. Object.entries(object.attrs).forEach(([key, value]) => parentElem.setAttribute(key, value));
  349. object.subTags.forEach(function (elem) {
  350. let elem1 = document.createElement(elem.tagName);
  351. parentElem.appendChild(elem1);
  352. console.log(parentElem);
  353. elem.subTags.forEach(function (inner) {
  354. let elem2 = document.createElement(inner.tagName);
  355. elem1.appendChild(elem2);
  356. elem2.textContent = inner.text;
  357. }
  358. );
  359. }
  360. );
  361. document.body.appendChild(parentElem);
  362. }
  363. createComponent(someTree);
  364. }
  365. const tasksArray = [
  366. ['different fields', runDifferentFields],
  367. ['fields check', runFieldsCheck],
  368. ['array of persons', runArrayOfPersons],
  369. ['loop of persons', runLoopOfPersons],
  370. ['loop of name and surname', runLoopOfNameAndSurname],
  371. ['loop of loop of values', runLoopOfLoopOfValues],
  372. ['fullName', runFullName],
  373. ['serialize', runSerialize],
  374. ['deserialize', runDeserialize],
  375. ['HTML', runHTML],
  376. ['HTML optional fields', runHTMLOptionalFields],
  377. ['HTML tr color', runHTMLTrColor],
  378. ['HTML th optional', runHTMLThOptional],
  379. ['destruct array', runDestructArray],
  380. ['destruct string', runDestructString],
  381. ['destruct 2', runDestruct2],
  382. ['destruct 3', runDestruct3],
  383. ['синий пояс', runBlueBelt]
  384. ];
  385. let $list = document.querySelector('.list');
  386. tasksArray.forEach(task => {
  387. const [name, callback] = task;
  388. const $div = document.createElement('div');
  389. $div.className = 'div';
  390. let $button = document.createElement('button');
  391. $button.textContent = 'Запустить';
  392. $button.className = 'button';
  393. $button.onclick = callback;
  394. $div.appendChild($button);
  395. const $li = document.createElement('li');
  396. $li.className = 'li';
  397. $li.textContent = name;
  398. $div.appendChild($li);
  399. $list.appendChild($div);
  400. });