task-03.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // destruct array
  2. // -------------УСЛОВИЕ-------------
  3. // let arr = [1,2,3,4,5, "a", "b", "c"]
  4. // напишите код, который используя деструктуризацию положит:
  5. // четные числа в переменные even1, even2,
  6. // нечетные в odd1, odd2, odd3,
  7. // буквы в отдельный массив
  8. // -------------РЕШЕНИЕ-------------
  9. const task16block = document.createElement('div');
  10. task16block.style = "border: 2px solid green; border-radius:5px; margin-bottom:10px; padding:10px";
  11. const task16title = document.createElement('h2');
  12. task16title.innerText = 'Task-16 Destruct array';
  13. const destructArrayBtn = document.createElement('button');
  14. destructArrayBtn.innerText = 'Destruct array';
  15. destructArrayBtn.style = 'margin-bottom:16px';
  16. root.appendChild(task16block);
  17. task16block.appendChild(task16title);
  18. task16block.appendChild(destructArrayBtn);
  19. destructArrayBtn.onclick = () => {
  20. let arr = [1, 2, 3, 4, 5, "a", "b", "c"];
  21. let [even1, even2, odd1, odd2, odd3, ...rest] = [...arr.filter(elem => !(elem % 2)&&!(isNaN(elem))), ...arr.filter(elem => elem % 2), arr.filter(elem=>isNaN(elem))];
  22. console.log([even1, even2, odd1, odd2, odd3, ...rest]);
  23. const task16even1 = document.createElement('p');
  24. const task16even2 = document.createElement('p');
  25. const task16odd1 = document.createElement('p');
  26. const task16odd2 = document.createElement('p');
  27. const task16odd3 = document.createElement('p');
  28. const task16letters = document.createElement('p');
  29. task16even1.innerText = `even1=${even1}`;
  30. task16even2.innerText = `even2=${even2}`;
  31. task16odd1.innerText = `odd1=${odd1}`;
  32. task16odd2.innerText = `odd2=${odd2}`;
  33. task16odd3.innerText = `odd3=${odd3}`;
  34. task16letters.innerText = `letters=[${[...rest]}]`;
  35. task16block.appendChild(task16even1);
  36. task16block.appendChild(task16even2);
  37. task16block.appendChild(task16odd1);
  38. task16block.appendChild(task16odd2);
  39. task16block.appendChild(task16odd3);
  40. task16block.appendChild( task16letters);
  41. }
  42. // -------------УСЛОВИЕ-------------
  43. // destruct string
  44. // let arr = [1, "abc"]
  45. // напишите код, который используя деструктуризацию положит:
  46. // число в переменную number
  47. // букву a в переменную s1
  48. // букву b в переменную s2
  49. // букву c в переменную s3
  50. // -------------РЕШЕНИЕ-------------
  51. const task17block = document.createElement('div');
  52. task17block.style = "border: 2px solid green; border-radius:5px; margin-bottom:10px; padding:10px";
  53. const task17title = document.createElement('h2');
  54. task17title.innerText = 'Task-17 Destruct string';
  55. const destructStringBtn = document.createElement('button');
  56. destructStringBtn.innerText = 'Destruct string';
  57. destructStringBtn.style = 'margin-bottom:16px';
  58. root.appendChild(task17block);
  59. task17block.appendChild(task17title);
  60. task17block.appendChild(destructStringBtn);
  61. destructStringBtn.onclick = () => {
  62. let arr = [1, "abc"]
  63. let [number, [s1, s2, s3]] = [...arr.filter(elem => !isNaN(elem)), ...arr.filter(elem => isNaN(elem))];
  64. console.log([number, [s1, s2, s3]]);
  65. const task17number = document.createElement('p');
  66. const task17s1 = document.createElement('p');
  67. const task17s2 = document.createElement('p');
  68. const task17s3 = document.createElement('p');
  69. task17number.innerText = `number=${number}`;
  70. task17s1.innerText = `s1=${s1}`;
  71. task17s2.innerText = `s2=${s2}`;
  72. task17s3.innerText = `s3=${s3}`;
  73. task17block.appendChild(task17number);
  74. task17block.appendChild(task17s1);
  75. task17block.appendChild(task17s2);
  76. task17block.appendChild( task17s3);
  77. }
  78. // -------------УСЛОВИЕ-------------
  79. // destruct 2
  80. // let obj = {name: 'Ivan',
  81. // surname: 'Petrov',
  82. // children: [{name: 'Maria'}, {name: 'Nikolay'}]}
  83. // извлеките используя деструктуризацию имена детей в переменные name1 и name2
  84. // -------------РЕШЕНИЕ-------------
  85. const task18block = document.createElement('div');
  86. task18block.style = "border: 2px solid green; border-radius:5px; margin-bottom:10px; padding:10px";
  87. const task18title = document.createElement('h2');
  88. task18title.innerText = 'Task-18 Destruct 2';
  89. const outputChildrenBtn = document.createElement('button');
  90. outputChildrenBtn.innerText = 'Output children names';
  91. outputChildrenBtn.style = 'margin-bottom:16px';
  92. root.appendChild(task18block);
  93. task18block.appendChild(task18title);
  94. task18block.appendChild(outputChildrenBtn);
  95. outputChildrenBtn.onclick = () => {
  96. let obj = {
  97. name: 'Ivan',
  98. surname: 'Petrov',
  99. children: [{ name: 'Maria' }, { name: 'Nikolay' }]
  100. }
  101. let [{ name: name1 }, {name:name2 }] = [...[...obj.children]];
  102. console.log(`name1:${name1}`)
  103. console.log(`name2:${name2}`)
  104. const task18childrenName1 = document.createElement('p');
  105. const task18childrenName2 = document.createElement('p');
  106. task18childrenName1.innerText = `name1:${name1}`;
  107. task18childrenName2.innerText = `name2:${name2}`;
  108. task18block.appendChild(task18childrenName1);
  109. task18block.appendChild(task18childrenName2);
  110. }
  111. // -------------УСЛОВИЕ-------------
  112. // destruct 3
  113. // let arr = [1,2,3,4, 5,6,7,10]
  114. // извлеките используя деструктуризацию объектов два первых элемента и длину массива в переменные a, b и length
  115. // -------------РЕШЕНИЕ-------------
  116. const task19block = document.createElement('div');
  117. task19block.style = "border: 2px solid green; border-radius:5px; margin-bottom:10px; padding:10px";
  118. const task19title = document.createElement('h2');
  119. task19title.innerText = 'Task-19 Destruct 3';
  120. const output2elemsAndLengthBtn = document.createElement('button');
  121. output2elemsAndLengthBtn.innerText = 'Output element1, element2 & array.length';
  122. output2elemsAndLengthBtn.style = 'margin-bottom:16px';
  123. root.appendChild(task19block);
  124. task19block.appendChild(task19title);
  125. task19block.appendChild(output2elemsAndLengthBtn);
  126. output2elemsAndLengthBtn.onclick = () => {
  127. let arr = [1, 2, 3, 4, 5, 6, 7, 10];
  128. let [[a, b], length] = [[...arr, ...arr], arr.length];
  129. console.log(`a:${a}`)
  130. console.log(`b:${b}`)
  131. console.log(`length:${length}`)
  132. const task19a = document.createElement('p');
  133. const task19b = document.createElement('p');
  134. const task19length = document.createElement('p');
  135. task19a.innerText = `a=${a}`;
  136. task19b.innerText = `b=${b}`;
  137. task19length.innerText = `length=${length}`;
  138. task19block.appendChild(task19a);
  139. task19block.appendChild(task19b);
  140. task19block.appendChild(task19length);
  141. }