hw5.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. // 3 persons ==============================================================================
  2. let personOne={
  3. name: '',
  4. surname: '',
  5. }
  6. let personTwo={
  7. name:'',
  8. surname:''
  9. }
  10. let personThree={
  11. name:'',
  12. surname:''
  13. }
  14. // different fields ==============================================================================
  15. let personOne={
  16. name: '',
  17. surname: '',
  18. age: '',
  19. fathername: '',
  20. sex: ''
  21. }
  22. let personTwo={
  23. name:'',
  24. surname:'',
  25. 'age': '',
  26. 'fathername': '',
  27. 'sex': ''
  28. }
  29. let personThree={
  30. name:'',
  31. surname:'',
  32. age:
  33. '',
  34. fathername:
  35. '',
  36. sex:
  37. ''
  38. }
  39. // fields check ==============================================================================
  40. let personOne={
  41. name: 'Vlad',
  42. surname: 'Stasenko',
  43. age: '27',
  44. fathername: 'Andriyovich',
  45. sex: ''
  46. }
  47. if ('sex' in personOne){
  48. alert(personOne.sex + ' Необязательное поле')
  49. }
  50. // либо =============================================
  51. if (typeof personOne.sex === 'string'){
  52. alert(personOne.sex + ' Необязательное поле')
  53. }
  54. let personTwo={
  55. name:'Kate',
  56. surname:'Stasenko',
  57. 'age': '',
  58. 'fathername': 'Andriivna',
  59. 'sex': 'woman'
  60. }
  61. if('age' in personTwo){
  62. alert(personTwo.age + ' Необязательное поле')
  63. }
  64. // либо =============================================
  65. if(typeof personTwo.age === 'string'){
  66. alert(personTwo.age + ' Необязательное поле')
  67. }
  68. let personThree={
  69. name:'Daniel',
  70. surname:'Deli',
  71. age:
  72. '27',
  73. fathername:
  74. '',
  75. sex:
  76. 'man'
  77. }
  78. if ('fathername' in personThree){
  79. alert(personThree.fathername + ' Необязательное поле')
  80. }
  81. // либо =============================================
  82. if(typeof personThree.fathername === 'string'){
  83. alert(personThree.fathername + ' Необязательное поле')
  84. }
  85. // array of persons ==============================================================================
  86. let persons = [
  87. personOne={
  88. name: '',
  89. surname: '',
  90. age: '',
  91. fathername: '',
  92. sex: ''
  93. },
  94. personTwo={
  95. name:'',
  96. surname:'',
  97. 'age': '',
  98. 'fathername': '',
  99. 'sex': ''
  100. },
  101. personThree={
  102. name:'',
  103. surname:'',
  104. age:
  105. '',
  106. fathername:
  107. '',
  108. sex:
  109. ''
  110. }
  111. ]
  112. // loop of persons ==============================================================================
  113. let persons = [
  114. personOne={
  115. name: '',
  116. surname: '',
  117. age: '',
  118. fathername: '',
  119. sex: ''
  120. },
  121. personTwo={
  122. name:'',
  123. surname:'',
  124. 'age': '',
  125. 'fathername': '',
  126. 'sex': ''
  127. },
  128. personThree={
  129. name:'',
  130. surname:'',
  131. age:
  132. '',
  133. fathername:
  134. '',
  135. sex:
  136. ''
  137. }
  138. ]
  139. for (let key in persons){
  140. console.log(persons[key]);
  141. }
  142. // либо =============================================
  143. let persons = [
  144. personOne={
  145. name: '',
  146. surname: '',
  147. age: '',
  148. fathername: '',
  149. sex: ''
  150. },
  151. personTwo={
  152. name:'',
  153. surname:'',
  154. 'age': '',
  155. 'fathername': '',
  156. 'sex': ''
  157. },
  158. personThree={
  159. name:'',
  160. surname:'',
  161. age:
  162. '',
  163. fathername:
  164. '',
  165. sex:
  166. ''
  167. }
  168. ]
  169. for (let i = 0; i<persons.length; ++i){
  170. console.log(persons[i]);
  171. }
  172. // loop of name and surname ==============================================================================
  173. let persons = [
  174. personOne={
  175. name: 'Vlad',
  176. surname: 'Stasenko',
  177. age: '',
  178. fathername: '',
  179. sex: ''
  180. },
  181. personTwo={
  182. name:'Kate',
  183. surname:'Stasenko',
  184. 'age': '',
  185. 'fathername': '',
  186. 'sex': ''
  187. },
  188. personThree={
  189. name:'Sveta',
  190. surname:'Stasenko',
  191. age:
  192. '',
  193. fathername:
  194. '',
  195. sex:
  196. ''
  197. }
  198. ]
  199. for (let key in persons){
  200. console.log(persons[key].name +' '+ persons[key].surname);
  201. }
  202. // loop of loop of values ==============================================================================
  203. let persons = [
  204. personOne={
  205. name: 'Vlad',
  206. surname: 'Stasenko',
  207. age: 27,
  208. fathername: 'Andriyovich',
  209. sex: 'man'
  210. },
  211. personTwo={
  212. name:'Kate',
  213. surname:'Stasenko',
  214. 'age': '27',
  215. 'fathername': 'Andriivna',
  216. 'sex': 'woman'
  217. },
  218. personThree={
  219. name:'Sveta',
  220. surname:'Stasenko',
  221. age:
  222. '48',
  223. fathername:
  224. 'Oleksiivna',
  225. sex:
  226. 'woman'
  227. }
  228. ]
  229. for(let i=0; i<(persons.length); i++ ){
  230. for(let key in persons[i]){
  231. console.log(persons[i][key])
  232. }
  233. }
  234. // fullName ==============================================================================
  235. let persons = [
  236. personOne={
  237. name: 'Vlad',
  238. surname: 'Stasenko',
  239. age: 27,
  240. fathername: 'Andriyovich',
  241. sex: 'man'
  242. },
  243. personTwo={
  244. name:'Kate',
  245. surname:'Stasenko',
  246. 'age': '27',
  247. 'fathername': 'Andriivna',
  248. 'sex': 'woman'
  249. },
  250. personThree={
  251. name:'Sveta',
  252. surname:'Stasenko',
  253. age:
  254. '48',
  255. fathername:
  256. 'Oleksiivna',
  257. sex:
  258. 'woman'
  259. }
  260. ]
  261. for(let i=0; i<(persons.length); i++ ){
  262. persons[i].fullName = (persons[i].name + ' ' + persons[i].surname + ' ' + persons[i].fathername)
  263. console.log(persons[i])
  264. }
  265. // serialize ==============================================================================
  266. let persons = [
  267. personOne={
  268. name: 'Vlad',
  269. surname: 'Stasenko',
  270. age: 27,
  271. fathername: 'Andriyovich',
  272. sex: 'man'
  273. },
  274. personTwo={
  275. name:'Kate',
  276. surname:'Stasenko',
  277. 'age': '27',
  278. 'fathername': 'Andriivna',
  279. 'sex': 'woman'
  280. },
  281. personThree={
  282. name:'Sveta',
  283. surname:'Stasenko',
  284. age:
  285. '48',
  286. fathername:
  287. 'Oleksiivna',
  288. sex:
  289. 'woman'
  290. }
  291. ]
  292. let json = JSON.stringify(persons)
  293. console.log(json)
  294. // deserialize ==============================================================================
  295. let persons = [
  296. personOne={
  297. name: 'Vlad',
  298. surname: 'Stasenko',
  299. age: 27,
  300. fathername: 'Andriyovich',
  301. sex: 'man'
  302. },
  303. personTwo={
  304. name:'Kate',
  305. surname:'Stasenko',
  306. 'age': 27,
  307. 'fathername': 'Andriivna',
  308. 'sex': 'woman'
  309. },
  310. personThree={
  311. name:'Sveta',
  312. surname:'Stasenko',
  313. age:
  314. 48,
  315. fathername:
  316. 'Oleksiivna',
  317. sex:
  318. 'woman'
  319. }
  320. ]
  321. let json = JSON.stringify(persons)
  322. let personFour = {"name": "Andrey", "surname": "Stasenko", "age": 48, "fathername": "Anatoliiovych", "sex": "man"}
  323. json= JSON.parse(persons.push(personFour))
  324. json=JSON.stringify(persons)
  325. console.log(persons)
  326. // HTML ==============================================================================
  327. let persons = [
  328. personOne={
  329. name: 'Vlad',
  330. surname: 'Stasenko',
  331. age: 27,
  332. fathername: 'Andriyovich',
  333. sex: 'man'
  334. },
  335. personTwo={
  336. name:'Kate',
  337. surname:'Yancheva',
  338. 'age': 27,
  339. 'fathername': 'Andriivna',
  340. 'sex': 'woman'
  341. },
  342. personThree={
  343. name:'Sveta',
  344. surname:'Maksimenko',
  345. age:
  346. 48,
  347. fathername:
  348. 'Oleksiivna',
  349. sex:
  350. 'woman'
  351. }
  352. ]
  353. let json = JSON.stringify(persons)
  354. let personFour = {"name": "Andrey", "surname": "Stasenko", "age": 48, "fathername": "Anatoliiovych", "sex": "man"}
  355. json= JSON.parse(persons.push(personFour))
  356. json=JSON.stringify(persons)
  357. var str = "<table border='1'>"
  358. for (let i=0;i<persons.length;i++){
  359. str += `<tr><td>${persons[i].name}</td><td>${persons[i].surname}</td></tr>`
  360. }
  361. str += "</table>"
  362. console.log(str)
  363. document.write(str)
  364. // HTML optional fields ==============================================================================
  365. let personFour
  366. let persons = [
  367. personOne={
  368. name: 'Vlad',
  369. surname: 'Stasenko',
  370. age: 27,
  371. fathername: 'Andriyovich',
  372. sex: 'man'
  373. },
  374. personTwo={
  375. name:'Kate',
  376. surname:'Yancheva',
  377. 'age': 27,
  378. 'fathername': 'Andriivna',
  379. 'sex': 'woman'
  380. },
  381. personThree={
  382. name:'Sveta',
  383. surname:'Maksimenko',
  384. age:
  385. 48,
  386. fathername:
  387. 'Oleksiivna',
  388. sex:
  389. 'woman'
  390. }
  391. ]
  392. let json = JSON.stringify(persons)
  393. personFour = {"name": "Andrey", "surname": "Stasenko", "age": 48, "fathername": "Anatoliiovych", "sex": "man"}
  394. json= JSON.parse(persons.push(personFour))
  395. json= JSON.stringify(persons)
  396. var str = "<table border='1'>"
  397. for(let key in persons[0]){
  398. str+= `<td>${key}</td>`
  399. }
  400. for (let i=0;i<persons.length;i++){
  401. str += `<tr><td>${persons[i].name}</td>
  402. <td>${persons[i].surname}</td>
  403. <td>${persons[i].age}</td>
  404. <td>${persons[i].fathername}</td>
  405. <td>${persons[i].sex}</td>
  406. </tr>`
  407. }
  408. str += "</table>"
  409. document.write(str)
  410. // HTML tr color ==============================================================================
  411. let personFour
  412. let persons = [
  413. personOne={
  414. name: 'Vlad',
  415. surname: 'Stasenko',
  416. age: 27,
  417. fathername: 'Andriyovich',
  418. sex: 'man'
  419. },
  420. personTwo={
  421. name:'Kate',
  422. surname:'Yancheva',
  423. 'age': 27,
  424. 'fathername': 'Andriivna',
  425. 'sex': 'woman'
  426. },
  427. personThree={
  428. name:'Sveta',
  429. surname:'Maksimenko',
  430. age:
  431. 48,
  432. fathername:
  433. 'Oleksiivna',
  434. sex:
  435. 'woman'
  436. }
  437. ]
  438. let json = JSON.stringify(persons)
  439. personFour = {"name": "Andrey", "surname": "Stasenko", "age": 48, "fathername": "Anatoliiovych", "sex": "man"}
  440. json= JSON.parse(persons.push(personFour))
  441. json= JSON.stringify(persons)
  442. var str = "<table border='1'>"
  443. for(let key in persons[0]){
  444. str+= `<td>${key}</td>`
  445. }
  446. for (let i=0;i<persons.length;i++){
  447. str += `<tr style="background: ${i%2?'#f5f5dc':'white'}" >
  448. <td>${persons[i].name}</td>
  449. <td>${persons[i].surname}</td>
  450. <td>${persons[i].age}</td>
  451. <td>${persons[i].fathername}</td>
  452. <td>${persons[i].sex}</td>
  453. </tr>`
  454. }
  455. str += "</table>"
  456. document.write(str)
  457. // HTML th optional ==============================================================================
  458. let personOne = {
  459. name: 'Vlad',
  460. surname: 'Stasenko',
  461. age: 27,
  462. fathername: 'Andriyovich',
  463. sex: 'man',
  464. car:'BMW',
  465. married: true
  466. }
  467. let personTwo = {
  468. name:'Kate',
  469. surname:'Yancheva',
  470. 'fathername': 'Andriivna',
  471. 'sex': 'woman'
  472. }
  473. let personThree = {
  474. name:'Sveta',
  475. surname:'Maksimenko',
  476. age:
  477. 48,
  478. fathername:
  479. 'Oleksiivna',
  480. sex:
  481. 'woman',
  482. married: false
  483. }
  484. let personFour
  485. let persons = [personOne, personTwo, personThree]
  486. let json = JSON.stringify(persons)
  487. personFour = {"name": "Andrey", "surname": "Stasenko", "age": 48, "fathername": "Anatoliiovych", "sex": "man"}
  488. json= JSON.parse(persons.push(personFour))
  489. json= JSON.stringify(persons)
  490. let keys = Object.keys(persons.reduce(function(keysPerson, obj){return Object.assign(keysPerson, obj);},0))
  491. let str = "<table border='1'>"
  492. for(let i=0; i<keys.length; ++i){
  493. str += `<th>${keys[i]}</th>`
  494. }
  495. for(let j=0; j<persons.length; ++j){
  496. str+='<tr>'
  497. for(k=0; k<keys.length; k++){
  498. str+=`<td style="background: ${j%2?'#f5f5dc':'white'}"> ${
  499. (persons[j][keys[k]]) == false? 'Не в браке':
  500. (persons[j][keys[k]]) == true? 'В браке' :
  501. (persons[j][keys[k]]) == undefined? 'Поле не заданно': (persons[j][keys[k]])
  502. } </td>`
  503. }
  504. str+='</tr>'
  505. }
  506. str += "</table>"
  507. document.write(str)
  508. // Задание на синий пояс ==============================================================================
  509. var someTree = {
  510. tagName: "table", //html tag
  511. subTags: [ //вложенные тэги
  512. {
  513. tagName: "tr",
  514. subTags: [
  515. {
  516. tagName: "td",
  517. text: "some text",
  518. },
  519. {
  520. tagName: "td",
  521. text: "some text 2",
  522. }
  523. ]
  524. }
  525. ],
  526. attrs:
  527. {
  528. border: 1,
  529. },
  530. }
  531. let strTop = `<${someTree.tagName}`
  532. let attrs = Object.keys(someTree.attrs)
  533. let values = Object.values(someTree.attrs)
  534. for (let i=0; i<attrs.length; i++){
  535. strTop= strTop + ' ' + attrs[i] + '=' + values[i]
  536. }
  537. strTop = strTop + ">"
  538. for ( j=0; j< someTree.subTags.length; j++){
  539. let openTR = '<'+ someTree.subTags[j].tagName + '>'
  540. let closeTR ='</'+ someTree.subTags[j].tagName + '>'
  541. strTop = strTop + openTR
  542. for(k=0; k<someTree.subTags[j].subTags.length; k++){
  543. let openTD = '<'+someTree.subTags[j].subTags[k].tagName+'>'
  544. let text = someTree.subTags[j].subTags[k].text
  545. let closeTD = '</'+someTree.subTags[j].subTags[k].tagName+'>'
  546. strTop=strTop+openTD+text+closeTD
  547. }
  548. strTop= strTop+closeTR
  549. }
  550. strTop=strTop+`</${someTree.tagName}>`
  551. console.log(strTop)
  552. // destruct array ==============================================================================
  553. let arr = [1,2,3,4,5, "a", "b", "c"]
  554. let [odd1,even1,odd2,even2,odd3,...words] = arr
  555. console.log(odd1)
  556. console.log(even1)
  557. console.log(odd2)
  558. console.log(even2)
  559. console.log(odd3)
  560. console.log(words)
  561. // destruct string ==============================================================================
  562. let arr = [1, "abc"]
  563. let [number, [s1,s2,s3]] = arr
  564. console.log(number)
  565. console.log(s1)
  566. console.log(s2)
  567. console.log(s3)
  568. // destruct 2 ==============================================================================
  569. let obj = {name: 'Ivan',
  570. surname: 'Petrov',
  571. children: [{name: 'Maria'}, {name: 'Nikolay'}]}
  572. let {children: [{name: name1}, {name: name2}]} = obj
  573. console.log(name1)
  574. console.log(name2)
  575. // destruct 3 ==============================================================================
  576. let arr = [1,2,3,4, 5,6,7,10]
  577. let ObjfromArr = {...arr}
  578. let ObjFromArr2 = {...ObjfromArr, length: arr.length}
  579. let {0: a, 1: b, length: length} = ObjFromArr2
  580. console.log(a)
  581. console.log(b)
  582. console.log(length)
  583. // ============================================================================================================================================================