Functions2.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // sort--------------------------------------------------------------------------------------------------------------------------;
  2. var persons = [
  3. { name: "Иван", age: 17 },
  4. { name: "Мария", age: 35 },
  5. { name: "Алексей", age: 73 },
  6. { name: "Яков", age: 12 },
  7. ];
  8. function byField(field, order) {
  9. var collator = new Intl.Collator(["en", "ru"], {
  10. numeric: true,
  11. });
  12. order = order ? 1 : -1;
  13. return function (a, b) {
  14. return order * collator.compare(a[field], b[field]);
  15. };
  16. }
  17. persons.sort(byField("age"));
  18. persons.sort(byField("name", false));
  19. // array map----------------------------------------------------------------------------------------------------------------------;
  20. {
  21. let prro = ["1", {}, null, undefined, "500", 700];
  22. let result = prro.map(function (item, index, arr) {
  23. var number = parseInt(item);
  24. return isNaN(number) ? item : number;
  25. });
  26. console.log(result);
  27. }
  28. // array reduce-------------------------------------------------------------------------------------------------------------------;
  29. {
  30. let arrReduce = ["0", 5, 3, "string", null];
  31. function numb(sum, i) {
  32. if (!isNaN(i) && isFinite(i) && i != 0 && i != null) {
  33. return (sum *= i);
  34. }
  35. return sum;
  36. }
  37. var result = arrReduce.reduce(numb, 1);
  38. console.log(result);
  39. }
  40. // object filter-------------------------------------------------------------------------------------------------------------------;
  41. {
  42. let laptop = {
  43. brand: "Lenovo",
  44. model: "Legion",
  45. RAM: "16 Gb",
  46. ROM: "512 Gb",
  47. processorModel: "Ryzen_5",
  48. numberOfCores: 6,
  49. };
  50. function filter(a, b, c) {
  51. for (var key in a) {
  52. if (key[0] === b) {
  53. delete a[key];
  54. } else if (a[key] === c) {
  55. delete a[key];
  56. }
  57. }
  58. return a;
  59. }
  60. console.log(filter(laptop, "b", 6));
  61. }
  62. // object map-------------------------------------------------------------------------------------------------------------------;
  63. function map(obj, funct) {
  64. for (key in obj) {
  65. funct(obj, key, obj[key]);
  66. delete obj[key];
  67. }
  68. return obj;
  69. }
  70. function newObj(obj, key, value) {
  71. obj[key + "^*&"] = value + "^^";
  72. return obj;
  73. }
  74. console.log(
  75. map({ brand: "Mercedes", model: "Vito", volume: 2.2, maxSpeed: 200 }, newObj)
  76. );
  77. // Рекурсия-------------------------------------------------------------------------------------------------------------------;
  78. // Sum-------------------------------------------------------------------------------------------------------------------;
  79. function sum(n) {
  80. if (n == 1) return 1;
  81. return n + sumTo(n - 1);
  82. }
  83. console.log(sum(55));
  84. // HTML Tree-------------------------------------------------------------------------------------------------------------------;
  85. let body = {
  86. tagName: "body",
  87. subTags: [
  88. {
  89. tagName: "div",
  90. subTags: [
  91. {
  92. tagName: "span",
  93. text: "Enter a data please:",
  94. },
  95. {
  96. tagName: "br",
  97. },
  98. {
  99. tagName: "input",
  100. attrs: {
  101. type: "text",
  102. id: "name",
  103. },
  104. },
  105. {
  106. tagName: "div",
  107. subTags: [
  108. {
  109. tagName: "input",
  110. attrs: {
  111. type: "text",
  112. id: "surname",
  113. },
  114. },
  115. {
  116. tagName: "div",
  117. subTags: [
  118. {
  119. tagName: "button",
  120. text: "Some text",
  121. attrs: {
  122. id: "newBtn",
  123. },
  124. },
  125. {
  126. tagName: "div",
  127. subTags: [
  128. {
  129. tagName: "div",
  130. subTags: [
  131. {
  132. tagName: "div",
  133. subTags: [
  134. {
  135. tagName: "input",
  136. attrs: {
  137. type: "date",
  138. id: "newDate",
  139. },
  140. },
  141. ],
  142. },
  143. ],
  144. },
  145. ],
  146. },
  147. ],
  148. },
  149. ],
  150. },
  151. ],
  152. },
  153. {
  154. tagName: "div",
  155. subTags: [
  156. {
  157. tagName: "button",
  158. text: "OK",
  159. attrs: {
  160. id: "ok",
  161. },
  162. },
  163. {
  164. tagName: "button",
  165. text: "cancel",
  166. attrs: {
  167. id: "cancel",
  168. },
  169. },
  170. ],
  171. },
  172. ],
  173. };
  174. function createElem(tagName, attrs, text) {
  175. var elem = document.createElement(tagName);
  176. if (attrs) for (var name in attrs) elem.setAttribute(name, attrs[name]);
  177. if (text) elem.textContent = text;
  178. return elem;
  179. }
  180. function walker(tree) {
  181. var tagName = tree.tagName,
  182. attrs = tree.attrs,
  183. text = tree.text,
  184. elem = createElem(tagName, attrs, text);
  185. if (tree.subTags)
  186. tree.subTags.forEach(function (el) {
  187. el = walker(el);
  188. elem.appendChild(el);
  189. });
  190. return elem;
  191. }
  192. var table = walker(body);
  193. document.body.appendChild(table);