main.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // 3 persons
  2. // array 1
  3. var a = {
  4. name: "Ivan",
  5. surname: "Ivanov",
  6. }
  7. // array 2
  8. var b = {
  9. name: "Taras",
  10. surname: "Tarasov",
  11. }
  12. // array 3
  13. var c = {
  14. name: "Petro",
  15. surname: "Petrov",
  16. }
  17. // different fields
  18. a.age = 33,
  19. b.sex = "male",
  20. c.fathername = "Petrovi4"
  21. // fields check
  22. // option 1
  23. let optionalField = ["age", "sex", "fathername"];
  24. let choiceArray = prompt("Выберите массив a, b, c: ");
  25. if (choiceArray === "a") {
  26. for (let i of optionalField) {
  27. if(i in a) {
  28. alert("Необязательное после для массива а: " + i);
  29. }
  30. }
  31. }
  32. else if (choiceArray === "b") {
  33. for (let i of optionalField) {
  34. if(i in b) {
  35. alert("Необязательное после для массива b: " + i);
  36. }
  37. }
  38. } else if (choiceArray === "c") {
  39. for (let i of optionalField) {
  40. if(i in c) {
  41. alert("Необязательное после для массива c: " + i);
  42. }
  43. }
  44. } else {
  45. alert("Ошибка!");
  46. }
  47. // option 2
  48. let choiceArray = prompt("Выберите массив a, b, c: ");
  49. let choiceArray1 = 'a';
  50. let choiceArray2 = 'b';
  51. let choiceArray3 = 'c';
  52. if(choiceArray === choiceArray1) {
  53. alert("Необязательное после для массива а: age");
  54. } else if(choiceArray === choiceArray2) {
  55. alert("Необязательное после для массива b: sex");
  56. } else if(choiceArray === choiceArray3) {
  57. alert("Необязательное после для массива c: fathername");
  58. } else {
  59. alert("Ошибка!");
  60. }
  61. // array of persons
  62. let persons = ['a', 'b', 'c'];
  63. let a = {
  64. name: "Ivan",
  65. surname: "Ivanov"
  66. }
  67. let b = {
  68. name: "Taras",
  69. surname: "Tarasov"
  70. }
  71. let c = {
  72. name: "Petro",
  73. surname: "Petrov"
  74. }
  75. persons.a = { name: "Ivan", surname: "Ivanov"};
  76. persons.b = { name: "Taras", surname: "Tarasov"};
  77. persons.c = { name: "Petro", surname: "Petrov"};
  78. // loop of persons
  79. for (let i in persons) {
  80. console.log(persons[i]);
  81. }
  82. // loop of name and surname
  83. for (var key in persons) {
  84. console.log(persons[key]);
  85. }
  86. // or
  87. for (let i in persons) {
  88. console.log(persons[i]["name"], persons[i]["surname"]);
  89. }
  90. // loop of loop of values
  91. for (let i in persons) {
  92. for (let j in persons[i]) {
  93. console.log(persons[i][j]);
  94. }
  95. }
  96. // fullName
  97. for (let i in persons) {
  98. if (persons[i]["fathername"]) {
  99. persons[i].fullName = persons[i]["name"] + persons[i]["fathername"] + persons[i]["surname"];
  100. } else {
  101. persons[i].fullName = persons[i]["name"] + persons[i]["surname"];
  102. }
  103. }
  104. // serialize
  105. let changeLine = JSON.strigify(persons);
  106. console.log(changeLine);
  107. // deserialize
  108. let changeArray = JSON.parse("[{\"name\":\"Ivan\",\"surname\":\"Ivanov\",\"age\":33,\"fullName\":\"IvanIvanov\"]");
  109. changeArray.push(persons);
  110. console.log(changeArray);
  111. // HTML
  112. //
  113. let persons = ['a', 'b', 'c'];
  114. let a = {
  115. name: "Ivan",
  116. surname: "Ivanov",
  117. }
  118. let b = {
  119. name: "Taras",
  120. surname: "Tarasov",
  121. }
  122. let c = {
  123. name: "Petro",
  124. surname: "Petrov",
  125. }
  126. persons = [a, b, c];
  127. //
  128. var str = "<table border='1'>"
  129. str += "<tr><th>Persons</th><th>name</th><th>surname</th></tr>"
  130. for(let i = 0; i < persons.length; i++) {
  131. if(i % 2 == 1) {
  132. str += `<tr><td>${i}</td>
  133. <td>${persons[i]["name"]}</td>
  134. <td>${persons[i]["surname"]}</td>`
  135. }
  136. else {
  137. str += `<tr><td>${i}</td>
  138. <td>${persons[i]["name"]}</td>
  139. <td>${persons[i]["surname"]}</td>`
  140. }
  141. }
  142. str += "</table>"
  143. document.write(str)
  144. // HTML optional fields
  145. //
  146. let persons = ['a', 'b', 'c'];
  147. let a = {
  148. name: "Ivan",
  149. surname: "Ivanov",
  150. age: 33,
  151. }
  152. let b = {
  153. name: "Taras",
  154. surname: "Tarasov",
  155. sex: "male",
  156. }
  157. let c = {
  158. name: "Petro",
  159. surname: "Petrov",
  160. fathername: "Petrovi4",
  161. }
  162. persons = [a, b, c];
  163. //
  164. let str = "<table border='1'>"
  165. str += "<tr><th>Persons</th><th>name</th><th>surname</th><th>age</th><th>fathername</th><th>sex</th></tr>"
  166. for(let i = 0; i < persons.length; i++) {
  167. if(i % 2 == 1) {
  168. str += `<tr><td>${i}</td>
  169. <td>${persons[i]["name"]}</td>
  170. <td>${persons[i]["surname"]}</td>
  171. <td>${persons[i]["age"]}</td>
  172. <td>${persons[i]["fathername"]}</td>
  173. <td>${persons[i]["sex"]}</td></tr>`
  174. }
  175. else {
  176. str += `<tr><td>${i}</td>
  177. <td>${persons[i]["name"]}</td>
  178. <td>${persons[i]["surname"]}</td>
  179. <td>${persons[i]["age"]}</td>
  180. <td>${persons[i]["fathername"]}</td>
  181. <td>${persons[i]["sex"]}</td></tr>`
  182. }
  183. }
  184. str += "</table>"
  185. document.write(str)
  186. // HTML tr color
  187. let str = "<table border='1'>"
  188. str += "<tr><th>Persons</th><th>name</th><th>surname</th><th>age</th><th>fathername</th><th>sex</th></tr>"
  189. for(let i = 0; i < persons.length; i++) {
  190. if(i % 2 == 1) {
  191. str += `<tr style='background: cyan'><td>${i}</td>
  192. <td>${persons[i]["name"]}</td>
  193. <td>${persons[i]["surname"]}</td>
  194. <td>${persons[i]["age"]}</td>
  195. <td>${persons[i]["fathername"]}</td>
  196. <td>${persons[i]["sex"]}</td></tr>`
  197. }
  198. else {
  199. str += `<tr style='background: yellow'><td>${i}</td>
  200. <td>${persons[i]["name"]}</td>
  201. <td>${persons[i]["surname"]}</td>
  202. <td>${persons[i]["age"]}</td>
  203. <td>${persons[i]["fathername"]}</td>
  204. <td>${persons[i]["sex"]}</td></tr>`
  205. }
  206. }
  207. str += "</table>"
  208. document.write(str)
  209. // Задание на синий пояс.
  210. var someTree = {
  211. tagName: "table",
  212. subTags: [{
  213. tagName: "tr",
  214. subTags: [{
  215. tagName: "td",
  216. text: "some text"
  217. }, {
  218. tagName: "td",
  219. text: "some text 2"
  220. }]
  221. }],
  222. attrs: {
  223. border: 1
  224. }
  225. }
  226. function createElem(tagName, attrs, text) {
  227. var elem = document.createElement(tagName);
  228. if (attrs)
  229. for (var name in attrs) elem.setAttribute(name, attrs[name]);
  230. if (text) elem.textContent = text;
  231. return elem
  232. }
  233. function walker(tree) {
  234. var tagName = tree.tagName,
  235. attrs = tree.attrs,
  236. text = tree.text,
  237. elem = createElem(tagName, attrs, text);
  238. if (tree.subTags) tree.subTags.forEach(function(el) {
  239. el = walker(el);
  240. elem.appendChild(el)
  241. });
  242. return elem
  243. }
  244. var table = walker(someTree);
  245. document.body.appendChild(table);
  246. // destruct array
  247. let arr = [1,2,3,4,5, "a", "b", "c"];
  248. let [odd1, odd2, odd3] = arr.filter(item => item%2);
  249. let [even1, even2] = arr.filter(item => !(item%2));
  250. let other = ["a", "b", "c"];
  251. // or
  252. let arr = [1,2,3,4,5, "a", "b", "c"];
  253. let [odd1, even1, odd2, even2, odd3, ...other] = arr;
  254. // or
  255. let [odd1, even1, odd2, even2, odd3, ...other] = new Set([1,2,3,4,5, "a", "b", "c"]);
  256. // destruct string
  257. let arr = [1, "abc"];
  258. let [number, [s1, s2, s3]] = arr;
  259. console.log(number, s1, s2,s3);
  260. // destruct 2
  261. let obj = {name: 'Ivan',
  262. surname: 'Petrov',
  263. children: [{name: 'Maria'}, {name: 'Nikolay'}]}
  264. let name1 = obj.children[0];
  265. let name2 = obj.children[1];
  266. // destruct 3
  267. let arr = [1,2,3,4, 5,6,7,10];
  268. let a = arr[0];
  269. let b = arr[1];
  270. let length = arr.length;