script.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Task1 3 persons
  2. //
  3. // const a = {
  4. // name: "Lena",
  5. // surname: "Ivanova",
  6. // }
  7. //
  8. // const b = {
  9. // name: "Kate",
  10. // surname: "Smit",
  11. // }
  12. //
  13. // const c = {
  14. // name: "Bob",
  15. // surname: "Petrov",
  16. // }
  17. //
  18. // // Task2 different fields
  19. //
  20. // a.age = 18;
  21. // a.fathername = "Makalester";
  22. //
  23. // b.sex = "femel";
  24. // b.age = "25";
  25. //
  26. // c.fathername = "Ivanovich"
  27. // c.sex = "mele";
  28. // Task3 Fields check
  29. // if(typeof a.age){
  30. // alert(a.age)
  31. // }
  32. // function check(obj, prop){
  33. // if(prop in obj){
  34. // alert(obj[prop]);
  35. // }
  36. // }
  37. // check(c, "sex");
  38. // Task4 array of persons
  39. //
  40. // const persons = [a, b, c, {name: "Pavel", surname: "Pavlov",}];
  41. //
  42. // // Task5 loop of persons
  43. //
  44. // for (let i = 0; i < persons.length; i++) {
  45. // console.log(persons[i]);
  46. // }
  47. //
  48. // // Task6 loop of name and surname
  49. //
  50. // for (let i = 0; i < persons.length; i++) {
  51. // console.log(persons[i].name, persons[i].surname);
  52. // }
  53. // Task7 loop of loop of value
  54. // for (let i=0; i<persons.length; i++){
  55. // for (let key in persons[i]){
  56. // console.log(persons[i][key]);
  57. // }
  58. // }
  59. // Task8 fullName
  60. // function fullName(persons){
  61. // for(let i=0; i<persons.length; i++){
  62. // persons[i].fullName =persons[i].name+ " " + persons[i].surname+" " +(persons[i].fathername || "");
  63. // console.log(persons[i]);
  64. // }
  65. // }
  66. // fullName(persons);
  67. // console.log(persons)
  68. // Task9 Serialize
  69. // JSON
  70. // `{"persons" : [
  71. // {"name": "Lena", "surname": "Ivanova", "age": "18", "fathername": "Makalester", "fullName": "Lena Ivanova Makalester"},
  72. // {"name": "Kat", "surname": "Smit", "sex": "femel", "age": "25", "fullName": "Kate Smit"},
  73. // {"name": "Bob", "surname": "Petrov", "fathername": "Ivanovich", "sex": "mele", "fullName": "Bob Petrov Ivanovich"},
  74. // {"name": "Pavel", "surname": "Pavlov", "fullName": "Pavel Pavlov "}
  75. // ]}`
  76. // Task10 deserialize
  77. // const json = `{"persons" : [
  78. // {"name": "Lena", "surname": "Ivanova", "age": "18", "fathername": "Makalester", "fullName": "Lena Ivanova Makalester"},
  79. // {"name": "Kat", "surname": "Smit", "sex": "femel", "age": "25", "fullName": "Kate Smit"},
  80. // {"name": "Bob", "surname": "Petrov", "fathername": "Ivanovich", "sex": "mele", "fullName": "Bob Petrov Ivanovich"},
  81. // {"name": "Pavel", "surname": "Pavlov", "fullName": "Pavel Pavlov "}
  82. // ]}`;
  83. // persons.push(JSON.parse(json).persons[0]);
  84. // console.log(persons)
  85. // // Task11 HTML
  86. // const json = `{"persons" : [
  87. // {"name": "Lena", "surname": "Ivanova", "age": "18", "fathername": "Makalester", "fullName": "Lena Ivanova Makalester"},
  88. // {"name": "Kat", "surname": "Smit", "sex": "femel", "age": "25", "fullName": "Kate Smit"},
  89. // {"name": "Bob", "surname": "Petrov", "fathername": "Ivanovich", "sex": "mele", "fullName": "Bob Petrov Ivanovich"},
  90. // {"name": "Pavel", "surname": "Pavlov", "fullName": "Pavel Pavlov "}
  91. // ]}`;
  92. // const persons = JSON.parse(json).persons;
  93. //
  94. // let str = "<table border='1' style='border-collapse: collapse'>"
  95. // for (let i=0; i<persons.length; i++){
  96. // str +=`<tr><td>${persons[i].name}</td><td>${persons[i].surname}</td></tr>`
  97. // }
  98. // str += "</table>"
  99. //
  100. // document.write(str)
  101. // Task12 HTML optional fields
  102. // +
  103. // Task 13
  104. // +
  105. // Task 14
  106. // const json = `{"persons" : [
  107. // {"name": "Lena", "surname": "Ivanova", "age": "18", "fathername": "Makalester", "fullName": "Lena Ivanova Makalester"},
  108. // {"name": "Kat", "surname": "Smit", "sex": "femel", "age": "25", "fullName": "Kate Smit"},
  109. // {"name": "Bob", "surname": "Petrov", "fathername": "Ivanovich", "sex": "mele", "fullName": "Bob Petrov Ivanovich"},
  110. // {"name": "Pavel", "surname": "Pavlov", "fullName": "Pavel Pavlov "}
  111. // ]}`;
  112. // const persons = JSON.parse(json).persons;
  113. // const headCaption = {
  114. // name: 'Name',
  115. // surname: 'Surname',
  116. // fathername: 'Fathername',
  117. // fullName: 'Full name',
  118. // age: 'Age',
  119. // sex: 'Sex',
  120. // }
  121. //
  122. //
  123. //
  124. // let str1 = "<table border='1' style=' border-collapse: collapse'>";
  125. // str1 +=`<tr>`;
  126. //
  127. // for (let key in headCaption){
  128. // str1+=`<th style="background-color: aquamarine; padding: 3px; text-align: center">${headCaption[key]}</th>`
  129. // }
  130. // str1 +=`</tr>`;
  131. // str1 +=`<tbody>`;
  132. //
  133. // for (let i = 0; i < persons.length; i++) {
  134. // str1 += `<tr style="background-color: ${i%2 ? 'red' : ""}">`;
  135. // for (let caption in headCaption){
  136. // str1+=`<td style="padding: 10px; text-align: center;">
  137. // ${persons[i][caption] ? persons[i][caption] : "-"}
  138. // </td>`
  139. // }
  140. //
  141. // str1 += `</tr>`;
  142. // }
  143. // str1 +=`</tbody>`;
  144. // str1 += `</table>`;
  145. //
  146. // document.write(str1);
  147. // Blue belt task
  148. const someTree = {
  149. tagName: "table", //html tag
  150. subTags: [ //вложенные тэги
  151. {
  152. tagName: "tr",
  153. subTags: [
  154. {
  155. tagName: "td",
  156. text: "some text",
  157. },
  158. {
  159. tagName: "td",
  160. text: "some text 2",
  161. },
  162. ]
  163. }
  164. ],
  165. attrs:
  166. {
  167. border: 1,
  168. },
  169. };
  170. console.log(Object.keys(someTree.attrs)[0])
  171. function createHtml(objTree) {
  172. let domStructure = '';
  173. domStructure += `<${objTree.tagName} ${Object.keys(objTree.attrs)[0]} : ${Object.values(objTree.attrs)[0]}>`;
  174. for (let i = 0; i < objTree.subTags.length; i++) {
  175. domStructure += `<${objTree.subTags[i].tagName}>`
  176. for (let j = 0; j < objTree.subTags[i].subTags.length; j++) {
  177. domStructure += `<${objTree.subTags[i].subTags[j].tagName}>`;
  178. domStructure += objTree.subTags[i].subTags[j].text;
  179. domStructure += `</${objTree.subTags[i].subTags[j].tagName}>`;
  180. }
  181. domStructure += `</${objTree.subTags[0].tagName}>`
  182. }
  183. domStructure += `</${objTree.tagName}>`
  184. document.write(domStructure);
  185. }
  186. createHtml(someTree);
  187. // Task blue belt destruct array
  188. let arr = [1, 2, 3, 4, 5, "a", "b", "c"];
  189. const [odd1, even1, odd2, even2, odd3, ...rest] = arr;
  190. //Task blue belt destruct string
  191. let arr1 = [1, "abc"];
  192. const [number, [s1,s2,s3]] = arr1;
  193. console.log(number , s3);
  194. // Task blue belt destruct2
  195. const obj = {name: 'Ivan',
  196. surname: 'Petrov',
  197. children: [{name: 'Maria'}, {name: 'Nikolay'}]}
  198. const {children: [{name: name1}, {name: name2}]}=obj;
  199. console.log(name1);
  200. console.log(name2);
  201. // Task blue belt destruct3
  202. let arr2 = [1,2,3,4, 5,6,7,10];
  203. const [a,b, ...c] = arr2;
  204. const [ , , length] =[ a,b, c.length]
  205. console.log( a, b, length);