script.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. "use strict";
  2. //TODO: 3 persons
  3. let a = {
  4. name: "Ivan",
  5. surname: "Petrov",
  6. }
  7. let b = {
  8. name: "Kate",
  9. surname: "Smirnova",
  10. }
  11. let c = {
  12. name: "Lisa",
  13. surname: "Volkova",
  14. }
  15. //TODO: different fields
  16. a.age = 16;
  17. a.sex = "male";
  18. b.fathername = "Aleksandrovna";
  19. c.age = 24;
  20. c.sex = "female";
  21. function fieldsCheck() {
  22. for (let key in a) {
  23. if (key == "name" || key == "surname") {
  24. continue
  25. }
  26. console.log(a[key]);
  27. }
  28. for (let key in b) {
  29. if (key == "name" || key == "surname") {
  30. continue
  31. }
  32. console.log(b[key]);
  33. }
  34. for (let key in c) {
  35. if (key == "name" || key == "surname") {
  36. continue
  37. }
  38. console.log(c[key]);
  39. }
  40. }
  41. // fieldsCheck()
  42. let persons = [a, b, c, {
  43. name: "Sara",
  44. surname: "Romanova",
  45. fathername: "Tihomirovna"
  46. }];
  47. function loopOfPersons() {
  48. for (let i = 0; i < persons.length; i++) {
  49. console.log(persons[i])
  50. };
  51. }
  52. // loopOfPersons()
  53. function loopOfNameSurname() {
  54. for (let key of persons) {
  55. console.log(`Name: ${key.name}, Surname: ${key.surname}`)
  56. }
  57. }
  58. // loopOfNameSurname()
  59. function loopOfValues() {
  60. for (let i in persons) {
  61. for (let key in persons[i]) {
  62. console.log(persons[i][key])
  63. }
  64. }
  65. }
  66. // loopOfValues()
  67. function fullName() {
  68. for (let obj of persons) {
  69. obj.fullName = `${obj.surname} ${obj.name}`;
  70. if (obj.fathername) {
  71. obj.fullName += ` ${obj.fathername}`;
  72. }
  73. console.log(obj);
  74. }
  75. }
  76. // fullName()
  77. function serialize() {
  78. let objJson = JSON.stringify(persons);
  79. }
  80. // serialize()
  81. function deserialize() {
  82. let obj = JSON.parse(`{"name":"Lara","surname":"Koval","age":12,"sex":"female"}`);
  83. persons.push(obj);
  84. console.log(persons);
  85. }
  86. // deserialize()
  87. function html() {
  88. let str = "<table border='1'>"
  89. for (let i = 0; i < persons.length; i++) {
  90. str +=
  91. `<tr>
  92. <td>${persons[i]["name"]}</td>
  93. <td>${persons[i]["surname"]}</td>
  94. </tr>`
  95. console.log(persons[i])
  96. }
  97. str += "</table>"
  98. console.log(str);
  99. document.write(str);
  100. }
  101. // html()
  102. function htmlOptionalFields() {
  103. let str = "<table border='1'>"
  104. for (let i = 0; i < persons.length; i++) {
  105. if ((i % 2) == 0) {
  106. str += "<tr bgcolor='grey'>"
  107. }
  108. for (let key in persons[i]) {
  109. str += `<td>${persons[i][key]}</td>`
  110. }
  111. str += "</tr>"
  112. console.log(persons[i]);
  113. }
  114. str += "</table>"
  115. console.log(str);
  116. document.write(str);
  117. }
  118. // htmlOptionalFields()
  119. function blueBelt() {
  120. let someTree = {
  121. tagName: "table",
  122. subTags: [{
  123. tagName: "tr",
  124. subTags: [{
  125. tagName: "td",
  126. text: "some text",
  127. },
  128. {
  129. tagName: "td",
  130. text: "some text",
  131. }
  132. ]
  133. }],
  134. attrs: {
  135. border: 1,
  136. },
  137. }
  138. let str = "<table border=1>"
  139. for (let i = 0; i < 1; i++) {
  140. str +=
  141. `<tr>
  142. <td>${"some text"}</td>
  143. <td>${"some text"}</td>
  144. </tr>`
  145. }
  146. str += "</table>"
  147. document.write(str)
  148. }
  149. // blueBelt()
  150. function destructArray() {
  151. let arr = [1, 2, 3, 4, 5, "a", "b", "c"];
  152. let [odd1, even1, odd2, even2, odd3, ...rest] = arr;
  153. console.log(rest);
  154. }
  155. // destructArray()
  156. function destructString() {
  157. let arr = [1, "abc"];
  158. let [number, [s1, s2, s3]] = arr;
  159. console.log(number);
  160. console.log(s1);
  161. console.log(s2);
  162. console.log(s3);
  163. }
  164. // destructString()
  165. function destruct2() {
  166. let obj = {
  167. name: 'Ivan',
  168. surname: 'Petrov',
  169. children: [{
  170. name: 'Maria'
  171. },
  172. {
  173. name: 'Nikolay'
  174. }
  175. ]
  176. }
  177. let [name1, name2] = [obj.children[0].name, obj.children[1].name];
  178. console.log(name1);
  179. console.log(name2);
  180. }
  181. // destruct2()
  182. function destruct3() {
  183. let arr = [1, 2, 3, 4, 5, 6, 7, 10];
  184. let [[a, b], length] = [arr, arr.length];
  185. console.log(a);
  186. console.log(b);
  187. console.log(length);
  188. }
  189. // destruct3()