script.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //html tree
  2. let obj = {
  3. body: {
  4. tagName: 'body',
  5. attrs: {},
  6. paired: true,
  7. subTags: [
  8. {
  9. tagName: 'div',
  10. attrs: {},
  11. paired: true,
  12. subTags: [
  13. {
  14. tagName: 'span',
  15. attrs: {},
  16. paired: true,
  17. text: 'Enter data please:',
  18. },
  19. {
  20. tagName: 'br',
  21. paired: false
  22. },
  23. {
  24. tagName: 'input',
  25. attrs: {
  26. type: 'text',
  27. id: 'name'
  28. },
  29. paired: false
  30. },
  31. {
  32. tagName: 'input',
  33. attrs: {
  34. type: 'text',
  35. id: 'surname'
  36. },
  37. paired: false
  38. }
  39. ]
  40. },
  41. {
  42. tagName: 'div',
  43. attrs: {},
  44. paired: true,
  45. subTags: [
  46. {
  47. tagName: 'button',
  48. attrs: {
  49. id: 'ok'
  50. },
  51. paired: true,
  52. text: 'Ok'
  53. },
  54. {
  55. tagName: 'button',
  56. attrs: {
  57. id: 'cancel'
  58. },
  59. paired: true,
  60. text: 'Cancel'
  61. },
  62. ],
  63. },
  64. ]
  65. }
  66. }
  67. console.log(`<button id='cancel'> innerText:`, obj.body.subTags[1].subTags[1].text)
  68. console.log(`<input type='text' id='surname'> id:`, obj.body.subTags[0].subTags[3].attrs.id)
  69. //declarative fields
  70. var notebook = {
  71. brand: prompt('Notebook brand:','') || "HP",
  72. type: prompt('Notebook type:','') || "440 G4",
  73. model: prompt('Notebook model:','') || "Y7Z75EA",
  74. ram: +prompt('Notebook ram:','') || 4,
  75. size: prompt('Notebook size:','') || "14",
  76. weight: +prompt('Notebook weight:','') ||1.8,
  77. resolution: {
  78. width: +prompt('Notebook width:','') || 1920,
  79. height: +prompt('Notebook height','') || 1080,
  80. },
  81. };
  82. console.log(notebook)
  83. var phone = {
  84. brand: prompt('Phone brand:','') || "meizu",
  85. model: prompt('Phone model:','') || "m2",
  86. ram: +prompt('Phone amount of ram:','') || 2,
  87. color: prompt('Phone color','') || "black",
  88. };
  89. console.log(phone)
  90. var person = {
  91. Name: prompt('Person name:','') || 'Donald',
  92. surname: prompt('Person surname:','') || "Trump",
  93. married: confirm('is married?'),
  94. }
  95. console.log(person)
  96. //object links
  97. notebook.owner = phone.owner = person
  98. person.phone = phone
  99. person.notebook = notebook
  100. console.log(phone, notebook)
  101. console.log(person.phone.owner.notebook.owner.phone == person.phone )
  102. //imperative array fill 3
  103. let imperatve = [prompt('input array[0] element','')||0, prompt('input array[1] element','')||1, prompt('input array[2] element','')||2]
  104. console.log(imperatve)
  105. //while confirm
  106. while(!confirm('break the loop?')){}
  107. //array fill
  108. let arrayFill = []
  109. for(let i = 0, str; i < arrayFill.length+1; i++) {
  110. str = prompt('input array element','')
  111. if(!str && str !== '') {
  112. break;
  113. } else {
  114. arrayFill.push(str)
  115. }
  116. }
  117. console.log(arrayFill)
  118. let arrayNoPush = []
  119. for(let i = 0, str2; i < arrayNoPush.length+1; i++) {
  120. str2 = prompt('input arrayNoPush element','')
  121. if(!str2 && str2 !== '') {
  122. break;
  123. } else {
  124. arrayNoPush[arrayNoPush.length] = str2
  125. }
  126. }
  127. console.log(arrayNoPush)
  128. //infinite probability
  129. let trys = 0
  130. while(true) {
  131. trys++
  132. let rand = Math.random()
  133. if(rand > 0.9) {
  134. alert(`Number of iterations: ${trys}`)
  135. break;
  136. }
  137. }
  138. //empty loop
  139. while(!prompt('Input smth and press OK to stop','')){}
  140. //progression sum
  141. let N = 10
  142. let step = 1
  143. let sum = 0
  144. for(let i = 1; i <= N; i++) {
  145. sum = sum + step
  146. step += 3
  147. }
  148. console.log('progression sum:', sum)
  149. //chess one line
  150. let strlen = 11
  151. let str = ''
  152. for(let i = 0; i < strlen; i++) {
  153. i % 2 === 0? str += ' ' : str += '#'
  154. }
  155. console.log(str, str.length)
  156. //numers
  157. let numbers = ''
  158. for(let i = 0; i < 10; i++) {
  159. for(let i = 0; i < 10; i++) {
  160. numbers += `${i}`
  161. }
  162. numbers += '\n'
  163. }
  164. console.log(numbers)