main.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //ES6
  2. let a = (text) => alert(text || "default");
  3. //a();
  4. //a("hello!");
  5. //
  6. var cube3 = (...args) => args.map((num = 1) => num ** 3);
  7. //console.log(cube3(4, 5, 6, 7))
  8. //console.log(cube3())
  9. let average2 = (...array) => (array.reduce((a, b) => (a + b / array.length), 0));
  10. //console.log('среднее число =', average2(10, 20, 30, 50));
  11. //console.log('среднее число =', average2());
  12. let sum = (...arg) => arg.reduce((a, b) => a + b, 0);
  13. //console.log('all sum = ', sum(1, 2, 3, 4, 5, 6))
  14. //console.log('all sum = ', sum())
  15. function greetAll() {
  16. let args = ["user"];
  17. for (let i = 0; i < arguments.length; i++) {
  18. args[i] = arguments[i];
  19. }
  20. return alert("Hello " + args);
  21. }
  22. //greetAll()
  23. //greetAll("Superman")
  24. //так же в дз с функциями было много вариантов написания функций
  25. //sort
  26. var persons = [
  27. { name: "Иван", age: 17 },
  28. { name: "Мария", age: 35 },
  29. { name: "Алексей", age: 73 },
  30. { name: "Яков", age: 12 },
  31. ]
  32. function sort(arr, field, value = true) {
  33. if (value) {
  34. arr.sort(((a, b) => a[field] > b[field] ? 1 : -1))
  35. } else {
  36. arr.sort(((a, b) => a[field] < b[field] ? 1 : -1))
  37. }
  38. }
  39. //sort(persons,'age',false);
  40. //console.log(persons);
  41. sort(persons, "name", false); //сортирует по имени по убыванию
  42. console.log(persons);
  43. //array map
  44. var array = ["1", {}, null, undefined, "500", 700]
  45. var result = array.map(function(item) {
  46. var number = Number(item);
  47. return isNaN(number) ? item : number
  48. })
  49. console.log(result);
  50. //array reduce
  51. var arr = ["0", 5, 3, "string", null]
  52. console.log(arr);
  53. var res = arr.reduce(function(a, b) {
  54. return (typeof(b) === 'number') ? (a *= b) : a;
  55. }, 1)
  56. console.log(res);
  57. //object filter
  58. var phone = {
  59. brand: "meizu",
  60. model: "m2",
  61. ram: 2,
  62. color: "black",
  63. };
  64. function filter(object, callback) {
  65. for (let key in object) {
  66. callback(key, object[key]) || delete object[key];
  67. }
  68. return object;
  69. }
  70. filter(phone, (key, value) => key == "color" || value == 2)
  71. console.log(phone)
  72. //object map
  73. function map(object, callback) {
  74. var obj = {};
  75. for (var key in object) Object.assign(obj, callback(key, object[key]));
  76. return obj
  77. }
  78. let new_obj = map({ name: "Иван", age: 17 }, function(key, value) {
  79. var result = {};
  80. result[key + "_"] = value + "$";
  81. return result;
  82. })
  83. console.log(new_obj) //должен вернуть {name_: "Иван$", age_: "17$"}
  84. function sumRec(number) {
  85. if (number === 1)
  86. return number
  87. if (number < 0)
  88. return
  89. return number + sumRec(number - 1)
  90. }
  91. console.log(sumRec(6))
  92. //HTML Tree
  93. var body = {
  94. tagName: 'body',
  95. subTags: [{
  96. tagName: 'div',
  97. subTags: [{
  98. tagName: 'span',
  99. text: "Enter a data please:",
  100. },
  101. {
  102. tagName: 'br',
  103. },
  104. {
  105. tagName: 'input',
  106. attrs: {
  107. type: 'text',
  108. id: 'name'
  109. }
  110. },
  111. {
  112. tagName: 'input',
  113. attrs: {
  114. type: 'text',
  115. id: 'surname'
  116. }
  117. }
  118. ],
  119. },
  120. {
  121. tagName: 'div',
  122. subTags: [{
  123. tagName: 'button',
  124. text: "OK",
  125. attrs: {
  126. id: 'ok'
  127. }
  128. },
  129. {
  130. tagName: 'button',
  131. text: "Cancel",
  132. attrs: {
  133. id: 'cancel'
  134. }
  135. },
  136. ],
  137. },
  138. ],
  139. }
  140. function createElem(tagName, attrs, text, id) {
  141. var tagElem = document.createElement(tagName);
  142. if (attrs)
  143. for (var name in attrs) tagElem.setAttribute(name, attrs[name]);
  144. if (text) tagElem.textContent = text;
  145. if (id) tagElem.id = id;
  146. return tagElem
  147. }
  148. function createBody(body) {
  149. var tagName = body.tagName,
  150. attrs = body.attrs,
  151. text = body.text,
  152. id = body.id,
  153. elem = createElem(tagName, attrs, text, id);
  154. if (body.subTags)
  155. body.subTags.forEach(function(el) {
  156. el = createBody(el);
  157. elem.appendChild(el)
  158. });
  159. return elem
  160. }
  161. var table = createBody(body);
  162. document.body.appendChild(table);